簡體   English   中英

保留原始y軸標題或調整通過yAxis標簽輸入的標題與y軸之間的距離

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

我想用幾個散點圖結合ggmatrixGGally(我更喜歡gridarrange因為更好的對齊時,y軸的標簽大小不同)。 我的問題是

  1. 是否可以保留原始軸標題(每個圖的y軸,僅最后一個圖的x-xis)?

或者

  1. 如果我使用yAxisLabels=ggmatrix添加y軸標題,可以修改(減少)軸標題和軸之間的空間嗎?
  2. 使用gridarrangegridarrange ,當y軸標簽的大小不同時 ,是否有辦法正確對齊多個圖的垂直網格線? (底部圖的大小可以通過在附加的圖例后添加圖例來進行調整,該圖例為組合的ggplots添加通用圖例,但這不適用於我的軸標簽/梯形圖)

示例代碼(我的原始數據集有更多行):

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM