我有一个DisplayPlot函数,它将一个字符串列表(它指的是数据框中的列)作为输入。 例如,如果list_string = c(“string1”,“string2”),循环list_string的元素,我想显示两个ggplots并排。R:并排显示功能内生成的可变数量的ggplots
到目前为止,我在我的功能所做的是存储ggplots成一个列表:
for (i in 1:length(list_string){
p = ggplot(data=d, aes(x=d[,1], y=as.numeric(levels(d[,(i+1)]))[d[,(i+1)]], fill=d[,1])) +
geom_bar(stat="identity") +
guides(fill=FALSE) +
labs(x = Y) + labs(y = paste("CP",list_X[i],"_per_",Y,sep = ""))
graph[[length(graph) + 1]] <- p }
,并返回:
return(list(graph=graph, along with some other information))
的问题是,如果我想使用的功能等grid.arrange
,我需要手动写出图的名字:grid.arrange(p1,p2,ncol=2)
而在我的情况下,这个数字可能会有所不同,并存储在一个列表中(grid.arrange
不喜欢的格式)。
预先感谢您非常的帮助, 克莱门特
作为一个侧面说明,你不应该使用'[ ''或'$'里面'aes' – baptiste
为什么我不应该在aes里面使用它们? – galzra
下面是一个可以出错的例子:http://stackoverflow.com/a/32543753/471093 – baptiste