在 R 中循环以创建并保存具有指定名称的一系列 ggplot2 绘图
我在 R 中有一个带有 POSIXct 变量 sessionstarttime 的数据框。每行由指定位置的整数 ID 变量标识。每个位置的行数不同。我简单地绘制总体图:
myplot <- ggplot(bigMAC, aes(x = sessionstarttime)) + geom_freqpoly()
是否可以创建一个循环来分别为每个位置创建和保存此类图?
最好文件名与 ID 变量的值相同?
最好每个图都有相同的时间尺度?
I have a data frame in R with POSIXct variable sessionstarttime. Each row is identified by integer ID variable of a specified location . Number of rows is different for each location. I plot overall graph simply by:
myplot <- ggplot(bigMAC, aes(x = sessionstarttime)) + geom_freqpoly()
Is it possible to create a loop that will create and save such plot for each location separately?
Preferably with a file name the same as value of ID variable?
And preferably with the same time scale for each plot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不完全确定你在问什么,但你可以做两件事之一。
a) 您可以将每个单独的绘图保存在一个循环中,并使用基于 ID 的唯一名称,如下所示:
b) 只需将每个绘图分配给列表的一个元素。然后使用
save
将该列表写入磁盘这将使以后加载和访问任何单独的图变得非常容易。
Not entirely sure what you're asking but you can do one of two things.
a) You can save each individual plot in a loop with a unique name based on ID like so:
b) Just assign each plot to an element of a list. Then write that list to disk using
save
That would make it very easy to load and access any individual plot at a later time.
我不确定我是否得到你想做的事。据我猜测,我建议编写一个简单的函数来保存绘图。然后使用 lapply(yourdata,yourfunction,...) 。由于 lapply 可用于列表,因此行数不必相等。
HTH
在您的函数中使用类似的内容:
I am not sure if I get what you want to do. From what I guess, i suggest to write a simple function that saves the plot. and then use lapply(yourdata,yourfunction,...) . Since lapply can be used for lists, it´s not necessary that the number of rows is equal.
HTH
use something like this in your function: