ggplot2:如何在图例的两条虚线之间添加间隔?
解决ggplot2图例虚线条目间隔问题
问题描述
我用以下代码生成了两幅直方图,希望找到简单方法在直方图图例的虚线之间添加间隔,避免虚线互相接触。我曾尝试在theme部分添加
legend.spacing.y,但仅实现了整个图例的下移,无法达成目标。
原代码
#make low cost histogram PLOT1 <- LCset1 %>% ggplot(aes(x=donation, fill=lowmonetaryamount))+ coord_cartesian(xlim = c(1, NA)) + geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+ scale_color_grey()+scale_fill_grey(start = .85, end = .85,) + theme_linedraw()+ guides(fill = "none", cols='none')+ geom_vline(aes(xintercept=10, size=' Low cost '), color="black", linetype=5)+ geom_vline(aes(xintercept=50, size=' High cost '), color="black", linetype="dotted")+ scale_size_manual(values = c(.5, 0.5), breaks = c(' Low cost ',' High cost '), guide=guide_legend(title = "", override.aes = list(linetype=c(5,3), color=c('black', 'black'))))+ scale_y_continuous(labels=scales::percent, limits = c(0, .015))+ scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c("","$10","$20","$30","$40","$50"," >$50"), limits = c(0, 60))+ ggtitle(" % donating each non-zero amount when low cost is $10")+ theme(plot.title = element_text(hjust = 0.5))+ xlab(NULL)+ ylab(NULL)+ theme(plot.title = element_text(size=13))+ theme( # other theme parameters legend.key.size = unit(2, "lines"), legend.key.width = unit(1, "lines"), legend.text = element_text(size=12), axis.text = element_text(size=12), legend.key.height = unit(0.5, "lines") ) #make high cost histogram PLOT2 <- HCset1 %>% ggplot(aes(x=donation, fill=lowmonetaryamount))+ coord_cartesian(xlim = c(1, NA)) + geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+ scale_color_grey()+scale_fill_grey(start = .85, end = .85,) + theme_linedraw()+ guides(fill = "none", cols='none')+ geom_vline(aes(xintercept=10, size=' Low cost '), color="black", linetype=5)+ geom_vline(aes(xintercept=50, size=' High cost '), color="black", linetype="dotted")+ scale_size_manual(values = c(.5, 0.5), breaks = c(' Low cost ',' High cost '), guide=guide_legend(title = "", override.aes = list(linetype=c(5,3), color=c('black', 'black'))))+ scale_y_continuous(labels=scales::percent, limits = c(0, .015))+ scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c("","$10","$20","$30","$40","$50"," >$50"), limits = c(0, 60))+ ggtitle("% donating each non-zero amount when high cost is $50")+ theme(plot.title = element_text(hjust = 0.5))+ xlab(NULL)+ ylab(NULL)+ theme(plot.title = element_text(size=13))+ theme( # other theme parameters legend.key.size = unit(2, "lines"), legend.key.width = unit(1, "lines"), legend.text = element_text(size=12), axis.text = element_text(size=12), legend.key.height = unit(0.5, "lines") ) #combine two histograms plot<- ggarrange(PLOT1, PLOT2,ncol=1,label.x=1,common.legend = TRUE, legend="right") plots <- grid.arrange(plot,top=textGrob("Histograms for the 40.1% of the total sample whose last donation was between $10.00 and $49.99",gp=gpar(fontsize=14,fontface="bold"))) ggsave("plots_1.png", plots, width = 10, height = 8)
解决方案
要让图例中的两条虚线条目分离,需要同时调整图例条目间距和每个图例key的垂直高度,还要清理代码中多余的换行:
- 调整图例间距与key高度:在
theme()中,设置legend.spacing.y为正数单位来增加条目间空隙,同时增大legend.key.height确保每个key有足够空间。 - 清理多余换行:移除
geom_vline和scale_size_manual中标签文本的多余换行,避免图例排版混乱。
修改后的核心代码片段
# 替换PLOT1和PLOT2中的theme部分 theme( legend.key.size = unit(2, "lines"), legend.key.width = unit(1, "lines"), legend.text = element_text(size=12), axis.text = element_text(size=12), legend.key.height = unit(1, "lines"), # 增大key的垂直高度 legend.spacing.y = unit(1, "lines") # 添加条目间的垂直间隔 ) # 替换geom_vline的size标签,移除多余换行 geom_vline(aes(xintercept=10, size='Low cost'), color="black", linetype=5)+ geom_vline(aes(xintercept=50, size='High cost'), color="black", linetype="dotted") # 替换scale_size_manual的breaks参数,移除多余换行 scale_size_manual( values = c(.5, 0.5), breaks = c('Low cost','High cost'), guide=guide_legend( title = "", override.aes = list(linetype=c(5,3), color=c('black', 'black')) ) )
修改后,图例中的两条虚线会被明显的垂直间隔分开,不会再互相接触。
内容的提问来源于stack exchange,提问作者Carter Allen
相关产品推荐
相关产品推荐

