遍历的列GGPLOT2(Looping over ggplot2 with columns)

2019-09-19 17:15发布

我试图遍历两个数据帧和列,以产生多条曲线。 我有数据帧的列表,并为每个,我想暗算几个指标之一的响应。

例如,我很容易跨数据帧循环:

df1=data.frame(response=rpois(10,1),value1=rpois(10,1),value2=rpois(10,1))
df2=data.frame(response=rpois(10,1),value1=rpois(10,1),value2=rpois(10,1))

#Looping across data frames
lapply(list(df1,df2), function(i) ggplot(i,aes(y=response,x=value1))+geom_point())

但我有麻烦的数据帧中的跨列循环:

lapply(list("value1","value2"), function(i) ggplot(df1,aes_string(x=i,y=response))+geom_point())

我怀疑它是与我治疗的美学的方式。

最后,我想串起来lapply生成数据帧和列的所有组合。

任何帮助表示赞赏!


编辑:Joran了吧! 我们必须把非清单的答复引号使用时aes_string

lapply(list("value1","value2"), function(i) ggplot(df1,aes_string(x=i,y="response"))+geom_point())

作为参考,这里串的lapply函数生成所有组合:

lapply(list(df1,df2), function(x)
  lapply(list("value1","value2"), function(i) ggplot(x,aes_string(x=i,y="response"))+geom_point() ) )

Answer 1:

里面的aes_string()所有的变量需要被表示为character 。 添加引号“响应”。

lapply(list("value1","value2"), 
       function(i) ggplot(df1, aes_string(x=i, y="response")) + geom_point())


文章来源: Looping over ggplot2 with columns
标签: r ggplot2