将两个ggplots与grid.arrange对齐

时间:2016-03-09 20:21:00

标签: r ggplot2

我想要两个ggplots对齐并且x轴长度相同。不知怎的,它不起作用。这是我的代码:

#5cm_Four治疗颜色不同

d1<-ggplot(MyDataMoistWinkler[550:10000,], aes(x=betterDate)) + 
          geom_line(aes(y=VerticalTillHigh5cm, color="Vertical Till High Disturbance 5cm"))+
          geom_line(aes(y=VerticalTillLow5cm, color="Vertical Till Low Disturbance 5cm"))+
          geom_line(aes(y=StripST5cm, color="Strip Till in row"))+
          geom_line(aes(y=StripNT5cm, color="Strip Till between row"))+
          geom_line(aes(y=Disc5cm, color="Double Disc"))+
          xlab("Date")+
          ylab("Volumetric Water Content [m3/m3]")+
          scale_y_continuous(limits = c(0, NA))+
          theme(text = element_text(size = 11, colour = "black"))+
          theme(legend.title=element_blank())+
          #theme(panel.background = element_blank(), panel.grid = element_line( size=.5, colour="black" ) )+
          theme(axis.text = element_text(size = 15, colour = "grey"))+# changes axis labels
          theme(axis.title = element_text(size = 15))+ # change axis titles
          theme(text = element_text(size = 15, colour = "black"))+ # this will change all text size # (except geom_text)
          theme(strip.text.x = element_text(size = 15, colour = "black", angle =NULL))+
          ggtitle("Moisture in 5cm, in Winkler MB")


d2<-ggplot(MyDataMoistWinkler[550:10000,], aes(x=betterDate, y=AccumulatedRainfall)) + 
              geom_line(aes(y=AccumulatedRainfall, color="Accumulated Rainfall"))+
              xlab("Date ")+
              ylab("Accumulated Rainfall [mm]")+
              scale_y_continuous(limits = c(0, NA))+
              theme(text = element_text(size = 11, colour = "black"))+
              theme(legend.title=element_blank())+
              #theme(panel.background = element_blank(), panel.grid = element_line( size=.5, colour="black" ) )+
              theme(axis.text = element_text(size = 15, colour = "grey"))+# changes axis labels
              theme(axis.title = element_text(size = 15))+ # change axis titles
              theme(text = element_text(size = 15, colour = "black"))+ # this will change all text size # (except geom_text)
              theme(strip.text.x = element_text(size = 15, colour = "black", angle =NULL))+
              ggtitle("Precipitation in mm, in Winkler MB")

第一选项:如果您想将它们彼此相邻排列

require(gridExtra)
grid.arrange(d1, d2, ncol=1)

1 个答案:

答案 0 :(得分:0)

library(grid)
grid.draw(rbind(ggplotGrob(d1), ggplotGrob(d2)))

可能有效