ggplot2 在组内按组在条形之间添加 space
ggplot2 adding space between bars by group within group
请看下面的代码。我正在尝试在图表中引入间距,以便按名称对正方形、三角形和圆形线进行分组,但我需要在正方形和三角形之间添加 space(因此三角形和圆形将是另一个更小的 "group" 在名称组内)。关于如何执行此操作的任何建议?我正在考虑添加另一个 "level",或者通过自定义躲避来完成,但不确定如何做到这一点。
## 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")
您可以通过在每对符号之间提供不同的闪避宽度来做到这一点。例如:
geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1)
根据您的评论,这是代码和结果图。我已将您的代码压缩为一个命令链,但它与您的问题中的代码相同,只是更改为 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")
请看下面的代码。我正在尝试在图表中引入间距,以便按名称对正方形、三角形和圆形线进行分组,但我需要在正方形和三角形之间添加 space(因此三角形和圆形将是另一个更小的 "group" 在名称组内)。关于如何执行此操作的任何建议?我正在考虑添加另一个 "level",或者通过自定义躲避来完成,但不确定如何做到这一点。
## 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")
您可以通过在每对符号之间提供不同的闪避宽度来做到这一点。例如:
geom_pointrange(position=position_dodge(width=c(0.7, 1.4)), size=1)
根据您的评论,这是代码和结果图。我已将您的代码压缩为一个命令链,但它与您的问题中的代码相同,只是更改为 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")