【发布时间】:2020-12-02 10:37:37
【问题描述】:
使用下面的示例数据,我想遍历我的数据框中的列以生成 ggplot,但我正在努力使逻辑正确(我是 R 新手)。我只想遍历其中包含短语“percent”的列。
这是我在一个列上生成单个 ggplot 的示例:
ggwatched10percent = ggplot(data = df1, aes(x=Duration, y=watched10percent))
ggwatched10percent + geom_point(aes(colour=factor(Content))) + ggtitle("Duration / viewed10percent Viewed")
ggsave(file.path('graphs', 'watched10percent.pdf'))
我正在寻找一个 for 循环,考虑到下面的数据,它将遍历 watch10percent、watched50percent 和 watch100percent 列(在每次迭代中,将始终使用 Duration 和 Content 列)。
给定的列将用作 y 值。我还需要将给定的列用作 ggsave 中的文件名,用于图表标题,也可能用作图表的变量(例如 ggwatched10ercent)——尽管我很乐意为此增加一个数字。
样本数据:
Content <- c('Part1','Part2','Part3')
Duration <- c(102, 205, 167)
watched10percent <- c('76','72','81')
watched50percent <- c('54','58','72')
watched100percent <- c('37','31','68')
df1 <- data.frame(Content, Duration, watched10percent, watched50percent, watched100percent)
编辑 - 我已经删除了我提供的数据样本......我得到的错误是因为我的数据没有被聚合,但是一旦聚合,提供的答案就完美了。
【问题讨论】: