ggplot2:在一个循环中在一页中打印多个图 [英] ggplot2 : printing multiple plots in one page with a loop
问题描述
我有几个主题需要为其生成图,因为我有很多主题,我希望在一页中具有多个图,而不是一个主题有一个图. 这是我到目前为止所做的:
I have several subjects for which I need to generate a plot, as I have many subjects I'd like to have several plots in one page rather than one figure for subject. Here it is what I have done so far:
读取带有主题名称的txt文件
Read txt file with subjects name
subjs <- scan ("ListSubjs.txt", what = "")
创建一个列表以保存绘图对象
Create a list to hold plot objects
pltList <- list()
for(s in 1:length(subjs))
{
setwd(file.path("C:/Users/", subjs[[s]])) #load subj directory
ifile=paste("Co","data.txt",sep="",collapse=NULL) #Read subj file
dat = read.table(ifile)
dat <- unlist(dat, use.names = FALSE) #make dat usable for ggplot2
df <- data.frame(dat)
pltList[[s]]<- print(ggplot( df, aes(x=dat)) + #save each plot with unique name
geom_histogram(binwidth=.01, colour="cyan", fill="cyan") +
geom_vline(aes(xintercept=0), # Ignore NA values for mean
color="red", linetype="dashed", size=1)+
xlab(paste("Co_data", subjs[[s]] , sep=" ",collapse=NULL)))
}
此时,我可以显示单个图,例如
At this point I can display the single plots for example by
print (pltList[1]) #will print first plot
print(pltList[2]) # will print second plot
我希望有一个解决方案,通过该解决方案可以在同一页面上显示多个图,我已经尝试了一些类似以前的文章,但是我没有设法使它起作用
I d like to have a solution by which several plots are displayed in the same page, I 've tried something along the lines of previous posts but I don't manage to make it work
例如:
for (p in seq(length(pltList))) {
do.call("grid.arrange", pltList[[p]])
}
给我以下错误
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!
我可以使用更多基本的图形功能,但是我想通过使用ggplot来实现.非常感谢您的考虑 马蒂尔德
I can use more basic graphing features, but I d like to achieve this by using ggplot. Many thanks for consideration Matilde
推荐答案
您的错误来自使用[[
为列表编制索引:
Your error comes from indexing a list with [[
:
考虑
pl = list(qplot(1,1), qplot(2,2))
pl[[1]]
返回第一个图,但是do.call
期望有一个 list 参数.您可以使用do.call(grid.arrange, pl[1])
来完成此操作(没有错误),但这可能不是您想要的(它在页面上排列了一个图,这样做毫无意义).想必您想要所有地块,
pl[[1]]
returns the first plot, but do.call
expects a list of arguments. You could do it with, do.call(grid.arrange, pl[1])
(no error), but that's probably not what you want (it arranges one plot on the page, there's little point in doing that). Presumably you wanted all plots,
grid.arrange(grobs = pl)
或等效地
do.call(grid.arrange, pl)
如果要选择此列表,请使用[
,
If you want a selection of this list, use [
,
grid.arrange(grobs = pl[1:2])
do.call(grid.arrange, pl[1:2])
可以使用第一种语法轻松地传递其他参数; do.call
必须注意确保列表的格式正确,
Further parameters can be passed trivially with the first syntax; with do.call
care must be taken to make sure the list is in the correct form,
grid.arrange(grobs = pl[1:2], ncol=3, top=textGrob("title"))
do.call(grid.arrange, c(pl[1:2], list(ncol=3, top=textGrob("title"))))
这篇关于ggplot2:在一个循环中在一页中打印多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!