我需要生成多个主题的图表,由于有很多主题,我希望在一页上有几个图表而不是一个主题一个图表。
以下是我迄今为止所做的:
读取包含主题名称的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
import(gridExtra)
)。 - Joseph Budin