我有几个主题需要生成一个情节,因为我有很多主题我想在一页中有几个情节而不是一个主题的图形。这是我到目前为止所做的:
读取带有主题名称的txt文件
subjs <- scan ("ListSubjs.txt", what = "")
创建一个列表来保存绘图对象
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)))
}
此时我可以显示单个图,例如
print (pltList[1]) #will print first plot
print(pltList[2]) # will print second plot
我想有一个解决方案,在同一页面上显示几个图,我已经尝试过一些类似以前帖子的方法,但我无法让它工作
例如:
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!
我可以使用更多基本的绘图功能,但我想通过使用 ggplot 来实现这一点。非常感谢考虑 Matilde