如何在两个图表ggplot 2上对齐y轴,以使它们具有相同的范围和增量
Posted
技术标签:
【中文标题】如何在两个图表ggplot 2上对齐y轴,以使它们具有相同的范围和增量【英文标题】:How to align y-axis on two graphs ggplot 2 so that they have the same range and increments 【发布时间】:2022-01-18 00:11:27 【问题描述】:我用 ggarrange 制作了一个有两个面板的人物。我希望两个面板具有相同的 y 轴范围和增量。即使使用 scale_y_continuous,我似乎也无法让它们正确排列。 Plot
这是我一直在使用的代码:
ei<- ggplot(data=ingestion, aes(x=Species, y=Numberofparticlesingested, colour=factor(Condition), shape=factor(Condition)))
ei1<- ei + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+
stat_summary(fun.y=mean, geom="point",size=2, position=position_dodge(width =0.90)) +
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom=
"errorbar", position=position_dodge(width=0.90), width=0.2) +scale_y_continuous(limits = c(0, 60))+
labs(x="Species", y="Average number of particles") +
theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))
ei2<- ei1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(strip.background = element_rect(fill="white"))+ theme(
axis.text.x = element_text(size = 14, vjust = -0.2, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans"))
e<- ggplot(data=egestioncumulativedata, aes(x=Time, y=Numberofparticles, colour=factor(Condition), shape=factor(Condition)))
e1<- e + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"))+
facet_grid(~Species) +
stat_summary(fun.y=mean, geom="point", size = 2, position=position_dodge(width =0.90)) + stat_summary(fun.y=mean, geom="line", size = 0.8, position=position_dodge(width =0.90))+
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom=
"errorbar", position=position_dodge(width=0.90), width=0.52) + scale_x_discrete(limits=c(6, 12, 18, 24, 30, 36, 42, 48)) + scale_y_continuous(limits = c(0, 60)) +
labs(x=expression(Time~ (Hours)), y=expression(Average ~number ~of ~particles)) +
theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))
e2<- e1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(strip.background = element_rect(fill="white"))+ scale_fill_discrete(breaks=c("AI","PV"))+ theme(
axis.text.x = element_text(size = 14, vjust = -0.25, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans"))
library(ggpubr)
figure6<-ggarrange(ei2,
e2+ theme(
axis.title.y = element_blank() ),
labels= c("a", "b"),
nrow = 1,ncol = 2,
align = "hv",
common.legend = TRUE, legend = "right")
【问题讨论】:
如果您包含一个简单的reproducible example 以及可用于测试和验证可能的解决方案的示例输入,则更容易为您提供帮助。 可能重复:***.com/questions/68540905/… 【参考方案1】:在scale_y_continuous
中使用breaks
和labels
参数以及limits
参数应该可以更好地控制轴
【讨论】:
我发现 library(patchwork) 也可以完美地对齐两个图。以上是关于如何在两个图表ggplot 2上对齐y轴,以使它们具有相同的范围和增量的主要内容,如果未能解决你的问题,请参考以下文章
在 highstock 上对齐 y 轴刻度“外部”,因此它们与 highcharts 上的相同