保留原始y轴标题或调整通过yAxis Labels和y轴输入的标题之间的距离

时间:2016-09-30 09:29:22

标签: r gridextra ggally

我想使用来自 GGally ggmatrix组合几个散点图(我更喜欢gridarrange,因为当y轴标签的大小不同时更好的对齐)。我的问题是

  1. 我可以保留原始轴标题(每个图表的y轴,x-xis只来自最后一个吗?
  2. 或者

    1. 我可以修改(减少)轴标题和轴之间的空间 我使用yAxisLabels=ggmatrix
    2. 中添加y轴标题
    3. 使用 Gridextra 中的gridarrange,当y轴标签大小不同时,有没有办法正确对齐多个绘图的垂直网格线? (可以通过在enter image description here之后添加额外的图例来调整底部图的大小,但这不适用于我的轴标签/标题)
    4. 示例代码(我的原始数据集包含更多行):

      library(ggplot2)
      library(gridExtra)
      library(lattice)
      library(grid)
      library(GGally)
      library(reshape2)
      
      clm <- data.frame(cbind(c(2004,2005,2006,2007,2008,2009,2010,2011,2012,2013),
               c(6.5,6.1,6.9,7.4,6.6,6.8,6.3,8.5,7.8,7.6),
               c(554.0,468.0,537.0,592.0,900.0,689.4,672.2,551.3,705.0,669.3)))
      colnames(clm) <- c("year","T","P")
      bai <- data.frame(cbind(c(2004,2005,2006,2007,2008,2009,2010,2011,2012,2013),
              c(336.4,489.5,309.8,141.5,913.4,604.1,791.1,1066.1,935.8,433.1),
              c(839.3,951.9,890.3,700.4,1636.2,1615.1,1259.3,1186.8,1888.5,1279.8),
              c(1169.2,1179.2,1275.1,1651.0,2127.1,2188.4,1740.7,1513.2,1826.2,2215.2)))
      colnames(bai) <- c("year","S1","S2","S3")
      chr <- data.frame(cbind(c(2004,2005,2006,2007,2008,2009,2010,2011,2012,2013),
              c(0.35,0.67,0.50,0.28,1.17,0.93,1.28,1.51,1.19,0.63),
              c(0.59,0.79,0.83,0.67,1.30,0.67,0.62,0.72,1.31,0.69),
              c(0.78,0.85,0.86,1.04,1.10,1.14,0.80,0.83,1.27,1.19)))
      colnames(chr) <- c("year","S1","S2","S3")
      
      l_bai <- melt(bai,id.vars = "year")
      l_chr <- melt(chr,id.vars = "year")
      
      # plot single plots
      gg_T <- ggplot(clm, aes(x=year, y=T)) + geom_line(colour = "red") + xlab("Year") + ylab("T (°C)")
      gg_T
      
      gg_P <- ggplot(clm, aes(x=year, y=P)) + geom_line(colour = "blue") +  xlab("Year") + ylab("P (mm)")
      gg_P
      
      gg_bai <- ggplot(l_bai, aes(x=year, y=value,colour = variable)) + geom_line()+ xlab("Year") + ylab("BAI (µm)")
      gg_bai
      
      gg_chr <- ggplot(l_chr, aes(x=year, y=value,colour = variable)) + geom_line()+ xlab("Year") + ylab("RWI (-)")
      gg_chr
      
      # combine plots with ggmatrix
      plotlist <- list(gg_T,gg_P,gg_bai,gg_chr)
      # no axis titles plotted
      ggmatrix(plotlist, 4, 1, showYAxisPlotLabels = TRUE,
           gg=theme(axis.title = element_text(size = 12)) + theme(axis.text=element_text(size = 10))) 
      # add axis titles with yAxisLabels/xAxisLabels => huge gap between y-axis labels and title
      ggmatrix(plotlist, 4, 1, showYAxisPlotLabels = TRUE,yAxisLabels = c("T (°C)","P (mm)"),xAxisLabels = "Year",
           gg=theme(axis.title = element_text(size = 12)) + theme(axis.text=element_text(size = 10))) 
      
      # alternative with gridarrange, title and text of x-axis, legends are not plotted in the first 3 plots
      gg_Tx <- ggplot(clm, aes(x=year, y=T)) + geom_line(colour = "red")+xlab("Year") + ylab("T (°C)") + theme(axis.title.x = element_blank(), axis.text.x = element_blank())
      gg_Tx
      
      gg_Px <- ggplot(clm, aes(x=year, y=P)) + geom_line(colour = "blue") + xlab("Year") + ylab("P (mm)") + theme(axis.title.x = element_blank(), axis.text.x = element_blank())
      gg_Px
      
      gg_baix <- ggplot(l_bai, aes(x=year, y=value,colour = variable)) + geom_line() + xlab("Year") + ylab("BAI (µm)") + theme(axis.title.x = element_blank(), axis.text.x = element_blank())+ guides(color=FALSE)
      gg_baix
      
      gg_chrx <- ggplot(l_chr, aes(x=year, y=value,colour = variable)) + geom_line()+ labs(colour="Site") + xlab("Year") + ylab("RWI (-)") + theme(legend.position = "bottom", legend.box = "horizontal")
      gg_chrx
      
      # problem with grid.arrange: vertical gridlines are not properly aligned due to different size of x-axis labels 
       grid.arrange(gg_Tx,gg_Px,gg_baix,gg_chrx,ncol=1)
      

1 个答案:

答案 0 :(得分:0)

你可以尝试

library(egg) # install_github('baptiste/egg')
ggarrange(gg_Tx,gg_Px,gg_baix,gg_chrx,ncol=1)

enter image description here