简体   繁体   English

将图形与grid.arrange组合并调整绘图大小和轴标签

[英]Combine plots with grid.arrange and adjust plot size and axis label

I want to combine two ggplots with grid.arrange with only one general legend. 我想将两个ggplotsgrid.arrange结合起来,只有一个常规图例。 I managed to combine the two legends with a small trick, but since I removed the legend from the first plot, this one is broader after grid.arrange , of course. 我设法将这两个传说与一个小技巧结合起来,但是自从我从第一个图中删除了这个图例,当然这个版本在grid.arrange之后更广泛。 How can I get both plotting areas to the same size? 如何将两个绘图区域设置为相同的大小? And also I would like to have one common x axis label centered below both plots. 而且我希望在两个图下方都有一个共同的x轴标签。 is that possible with grid.arrange ? grid.arrange是可能的吗? I know, similar questions have been answered before, but I'm still a newbie to R and the solutions are too complicated or I cannot fit them to my data. 我知道,之前已经回答了类似的问题,但我仍然是R的新手,解决方案太复杂,或者我无法将它们与我的数据相匹配。

So here are my two datasets: 所以这是我的两个数据集:

testxy
  SN strain     est     low      up
1  A     xy 11.6751 11.1480 12.2021
2  B     xy 11.4211 11.1108 11.7314
3  C     xy  2.6603  2.4291  2.8915
4  D     xy  4.5503  4.2972  4.8034

testyz
  SN strain     est     low      up
5  A     yz 22.1761 21.5136 22.8387
6  C     yz 21.4829 21.0251 21.9408
7  B     yz 19.3294 18.8950 19.7639
8  D     yz 19.9990 19.3934 20.6047

And this is the code I have so far. 这是我到目前为止的代码。 It's close to what I want, but just close: 它接近我想要的,但只是关闭:

p1<-ggplot(data=testxy, aes(colour=strain, x=SN, y=est))+
theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
theme(legend.position="none")+
theme(axis.title.x = element_text(size = rel(1.5), vjust=-0.1), 
axis.title.y = element_text(size = rel(1.5), vjust=1), axis.text.y = element_text(size = rel(1.4)), axis.text.x = element_text(hjust = 1, size = rel(1.5)),plot.title = element_text(size = rel(2.5), lineheight=1, face="bold"))+
      theme(plot.margin=unit(c(5,5,5,5),"mm"))+
      labs(x="treatment", y="integral", title="xy")+
      scale_colour_manual(name="strain", values=c(xy="blue"))+
      theme(strip.text.x = element_text(size=12, face="bold"), strip.background = element_rect(colour="black", fill="white"))+
      geom_point(aes(color="xy"), size=5, alpha=0.1, shape=16)+
      geom_errorbar(aes(ymin=low, ymax=up, width=0.2), colour="deepskyblue", size=0.8)+
      scale_y_continuous(breaks=seq(5,20,5), limits=c(2,23.5))

p2<-ggplot(data=testyz, aes(colour=strain, x=SN, y=est))+
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
  theme(legend.position="right")+
  theme(axis.title.x = element_text(size = rel(1.5), vjust=-0.1), axis.ticks.y = element_blank(), axis.text.y = element_blank(), axis.text.x = element_text(hjust = 1, size = rel(1.5)), plot.title = element_text(size = rel(2.5), lineheight=1, face="bold"))+
  theme(plot.margin=unit(c(5,5,5,5),"mm"))+
  labs(x="treatment", y=NULL, title="yz")+
  scale_colour_manual(name="strain", values=c(yz="green", xy="blue"))+
  theme(strip.text.x = element_text(size=12, face="bold"), strip.background = element_rect(colour="black", fill="white"))+
  geom_point(aes(color="yz"), size=5, alpha=0.1, shape=16)+
  geom_point(aes(color="xy"), size=0)+
  geom_errorbar(aes(ymin=low, ymax=up, width=0.2), colour="green", size=0.8)+
  scale_y_continuous(breaks=seq(5,20,5), limits=c(2,23.5))+
  scale_x_discrete(limits=c("A", "C", "B", "D"))

grid.arrange(p1,p2, ncol=2)

I tried facetting before. 我以前试过过facetting。 It looks really good, but unfortunately, I need to change the order of the levels on the x axes. 它看起来非常好,但不幸的是,我需要改变x轴上的levels顺序。 So, I think facetting doesn't work for me. 所以,我认为facetting对我不起作用。

I hope you can help me. 我希望你能帮助我。

Cheers Anne 干杯安妮

You should use facetting: 你应该使用facetting:

testxy <- read.table(text = "  SN strain     est     low      up
1  A     xy 11.6751 11.1480 12.2021
2  B     xy 11.4211 11.1108 11.7314
3  C     xy  2.6603  2.4291  2.8915
4  D     xy  4.5503  4.2972  4.8034", header = TRUE)

testyz <- read.table(text = "   SN strain     est     low      up
5  A     yz 22.1761 21.5136 22.8387
6  C     yz 21.4829 21.0251 21.9408
7  B     yz 19.3294 18.8950 19.7639
8  D     yz 19.9990 19.3934 20.6047", header = TRUE)

test <- rbind(cbind(testxy, fac = "xy"),
              cbind(testyz, fac = "yz"))


test$SN1 <- interaction(test$SN, test$fac)
test$SN1 <- ordered(test$SN1, levels = test$SN1)


ggplot(data=test, aes(colour=strain, x=SN1, y=est)) +
  geom_point() + 
  facet_wrap(~ fac, scales = "free_x") +
  scale_x_discrete(labels = setNames(as.character(test$SN), as.character(test$SN1))) 

结果情节

However, this is not a good plot, since the reader will often not notice that the x-axes are different. 然而,这不是一个好的情节,因为读者通常不会注意到x轴是不同的。

If the only issue of facetting is the order of levels, it can be resolved by reordering your factor-variable prior to calling ggplot2. 如果facetting的唯一问题是级别的顺序,可以通过在调用ggplot2之前重新排序factor-variable来解决它。

You can specify the order of levels of a factor when converting to a factor. 转换为因子时,您可以指定因子的级别顺序。

df = rbind(testxy, testyz)
df$strain <- factor(as.character(df$strain), levels=c('yz','xy'))
ggplot(df, aes(colour=strain, x=SN, y=est))+facet_grid(.~strain)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM