ggplots的数据:
set.seed(0)
library(ggplot2)
library(gridExtra)
c <- list()
for (k in 1:9) c[[k]] <- ggplot(data.frame(x=1:10,y=rnorm(10)),aes(x=x,y=y))+geom_line()
grid.arrange (c[[1]],c[[2]],c[[3]],c[[4]],c[[5]]
,c[[6]],c[[7]],c[[8]],c[[9]],ncol=3, nrow=3, widths = c(4,4,4) ,heights = c(4,4,4))
我想要每行和每列的标题。
输出的形状如下:
CTitle 1 CTitle 2 CTitle 3
RTitle1 plot1 plot2 plot3
RTitle2 plot4 plot5 plot6
RTitle3 plot7 plot8 plot9
答案 0 :(得分:4)
您可以为每个列/行使用嵌套的arrangeGrob调用,设置top和left参数。像这样:
grid.arrange (arrangeGrob(c[[1]], top="CTitle1", left="RTitle1"),arrangeGrob(c[[2]],top="CTitle2"),arrangeGrob(c[[3]],top="CTittle3"),arrangeGrob(c[[4]], left="RTitle2"),c[[5]],c[[6]],arrangeGrob(c[[7]],left="RTitle3"),c[[8]],c[[9]],ncol=3, nrow=3, widths = c(4,4,4) ,heights = c(4,4,4))
以下是使用@ eipi10
简化流程的代码# Create list of plots
set.seed(0)
pl = lapply(1:9, function(i) {
p = ggplot(data.frame(x=1:10,y=rnorm(10)),aes(x, y)) +
geom_line()
})
# Create row and column titles
col.titles = paste("C_Title", 1:3)
row.titles = paste("R_Title", 4:6)
# Add row titles
pl[1:3] = lapply(1:3, function(i) arrangeGrob(pl[[i]], left=row.titles[i]))
# Add column titles and lay out plots
grid.arrange(grobs=lapply(c(1,4,7), function(i) {
arrangeGrob(grobs=pl[i:(i+2)], top=col.titles[i/3 + 1], ncol=1)
}), ncol=3)
答案 1 :(得分:2)
这是另一种选择:
pl <- replicate(12, ggplot(), FALSE)
N <- length(pl)
nr <- 4
nc <- 3
combine <- rbind(tableGrob(t(c(letters[1:nc])), theme = ttheme_minimal(), rows = ""),
cbind(tableGrob(LETTERS[1:nr], theme = ttheme_minimal()),
arrangeGrob(grobs = pl), size = "last"), size = "last")
grid.newpage()
grid.draw(combine)