保留原始 y-axis 个标题或调整通过 yAxis 标签输入的标题与 y 轴之间的距离

Keep original y-axis titles or adjust distance between titles entered via yAxis Labels and the y axis

我想使用 GGally 中的 ggmatrix 组合几个散点图(我更喜欢 gridarrange 因为当 y-axis 标签大小不同)。我的问题是

  1. 我可以保留原始坐标轴标题(每个绘图的y-axis,最后一个绘图的x-xis吗?

或者

  1. 我可以修改(减少)轴标题和轴之间的 space 如果 我使用 yAxisLabels=ggmatrix?
  2. 中添加 y-axis 个标题
  3. 使用 Gridextra 中的 gridarrange,当 y-axis 标签大小不同时,有没有办法正确对齐多个绘图的垂直网格线? (底部图的大小可以通过在 Add a common Legend for combined ggplots 之后添加额外的图例来调整,但这对我来说不适用于轴 labels/titels)

示例代码(我的原始数据集有更多行):

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)

你可以试试

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