Please see code below. I am trying to introduce spacing in the graph such that a square, a triangle, and a circle lines are grouped by name, but I need additional space between square and triangle (so triangle and circle would be another smaller "group" within the name group). Any suggestions on how to do this? I'm thinking add another "level", or do it by custom dodging, but not sure how to do this.
## Data
names <- c("bio", "bio", "bio", "coal", "coal", "coal")
upper <- c(1.02, 1.08, 1.20, 1.03, 1.04, 1.05)
lower <- c(0.96, 1.02, 1.0, 1.01, 1.02, 1.03)
coef <- c(0.99, 1.05, 1.11, 1.02, 1.03, 1.04)
level <- c(1,2,3,1,2,3)
level2 <- c(1,2,3,4,5,6)
overall <- data.frame(names,upper,lower,coef,level,level2)
overall$level <- as.factor(overall$level)
overall$level2 <- as.factor(overall$level2)
## Graph
graph <- ggplot(overall, aes(x=reorder(names, level2), y=coef, ymin=lower, ymax=upper, shape=level)) + geom_pointrange(position=position_dodge(width=.7), size=1)
## Lines & Labels
graph <- graph + geom_hline(aes(yintercept=1), linetype='dashed') + ylab("HR") + xlab("")
graph <- graph + theme(panel.background = element_rect(fill='white', colour='black')) + theme(axis.text.x = element_text(angle=40, hjust=1, size=11, colour='black'), axis.text.y=element_text(size=11, colour='black'))
## Legend
graph <- graph + coord_flip()
graph <- graph + scale_colour_discrete(guide = FALSE) + theme(legend.text.align = 0) + theme(legend.title=element_blank())
graph + theme(legend.position="right")
You can do this by providing different dodge widths between each pair of symbols. For example:
geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1)
Per your comment, here's the code and the resulting graph. I've condensed your code into a single chain of commands, but it's the same code as in your question, except for the change to geom_pointrange
:
ggplot(overall, aes(x=reorder(names, level2),
y=coef, ymin=lower, ymax=upper, shape=level)) +
geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1) +
geom_hline(aes(yintercept=1), linetype='dashed') + ylab("HR") + xlab("") +
theme(panel.background = element_rect(fill='white', colour='black')) +
theme(axis.text.x = element_text(angle=40, hjust=1, size=11, colour='black'),
axis.text.y=element_text(size=11, colour='black')) +
coord_flip() +
scale_colour_discrete(guide = FALSE) +
theme(legend.text.align = 0,
legend.title=element_blank(),
legend.position="right")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments