我正在尝试在pdf中输出两个 ggplot 图表列表。我想制作一个pdf文件,其中左边有一个列表,右边有一个列表(理想情况下是4个2格的多页)。
我包含所有代码只是为了彻底,但重要的部分是绘图。
如果我在R studio中运行它,我会在图表预览中看到我想要的内容,但如果我包含在 pdf(文件名)和 dev.off <之间打印图表的循环/ em>我得到一个单页的pdf文件,其中所有的图都在彼此之上。我到处都看,但我无法让它发挥作用。
library(ggplot2)
library(tools)
library(gridExtra)
library(grid)
data_ahl<-read.csv("data_AHL.txt", header = TRUE)
data_none<-read.csv("data_none.txt", header = TRUE)
data_ahl$Concentration <- as.factor(data_ahl$Concentration)
data_none$Concentration <- as.factor(data_none$Concentration)
data <- rbind(data_ahl, data_none)
plasmid_list <- unique(data_ahl$Plasmid)
plot_list_atc <- list()
plot_list_none <- list()
j = 1
for (receiver in plasmid_list){
print(receiver)
upperbound <- max(data_ahl$GFP)
a <- subset(data_ahl, Plasmid == receiver)
plot_atc <- ggplot(a, aes(x = GFP)) +
geom_density(color = 'green') +
ggtitle(label = c(receiver, "induced with aTC")) +
xlab("GFP") +
ylab("Density") +
theme_minimal() +
scale_x_continuous(limits = c(0,upperbound))
b <- subset(data_none, Plasmid == receiver)
plot_none <- ggplot(b, aes(x = GFP)) +
geom_density(color = 'green') +
labs(title = c(receiver, "no induction")) +
xlab("GFP") +
ylab("Density") +
theme_minimal() +
scale_x_continuous(limits = c(0,upperbound))
plot_list_atc[[j]] <- plot_atc
plot_list_none[[j]] <- plot_none
j = j+1
}
#----PLOTTING-----
grid.newpage()
main.vp <- viewport(layout = grid.layout(11,2))
pushViewport(main.vp)
pdf("receiver_plots.pdf", paper = "a4")
nplots <- length(plot_list_atc)
row = 1
for (i in 1:nplots){
print(plot_list_none[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 1))
print(plot_list_atc[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 2))
row = row + 1
}
dev.off()
答案 0 :(得分:0)
您必须先打开pdf设备,然后布局网格:
#----PLOTTING-----
pdf("receiver_plots.pdf", paper = "a4")
grid.newpage()
main.vp <- viewport(layout = grid.layout(11,2))
pushViewport(main.vp)
nplots <- length(plot_list_atc)
row = 1
for (i in 1:nplots){
print(plot_list_none[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 1))
print(plot_list_atc[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 2))
row = row + 1
}
dev.off()
答案 1 :(得分:0)
也许您可以尝试multiplot功能我正在使用。
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
p1到p5是我的ggplot对象,在一列中对齐
multiplot(p1,p2,p3,p4,p5,cols=1)
答案 2 :(得分:0)
感谢大家的回答,我最终设法解决了我的问题。显然,在声明视口之前必须打开pdf设备。我还设法以4乘2格的形式制作了这些地块。
我包含代码以防有人发现它有用。
#----PLOTTING-----
pdf("receiver_plots.pdf", width = 10, height = 15)
grid.newpage()
main.vp <- viewport(layout = grid.layout(4,2))
pushViewport(main.vp)
nplots <- length(plot_list_atc)
row = 1
for (i in 1:nplots){
print(plot_list_none[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 1))
print(plot_list_atc[[i]], vp = viewport(layout.pos.row = row,
layout.pos.col = 2))
if (row == 4){
grid.newpage()
main.vp <- viewport(layout = grid.layout(4,2))
pushViewport(main.vp)
row = 1
} else {
row = row + 1
}
}
dev.off()