在 R 中循环以创建并保存具有指定名称的一系列 ggplot2 绘图

发布于 2024-09-08 18:07:14 字数 268 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-09-15 18:07:14

不完全确定你在问什么,但你可以做两件事之一。

a) 您可以将每个单独的绘图保存在一个循环中,并使用基于 ID 的唯一名称,如下所示:

ggsave(myplot,filename=paste("myplot",ID,".png",sep="")) # ID will be the unique identifier. and change the extension from .png to whatever you like (eps, pdf etc).

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:

ggsave(myplot,filename=paste("myplot",ID,".png",sep="")) # ID will be the unique identifier. and change the extension from .png to whatever you like (eps, pdf etc).

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.

幸福还没到 2024-09-15 18:07:14

我不确定我是否得到你想做的事。据我猜测,我建议编写一个简单的函数来保存绘图。然后使用 lapply(yourdata,yourfunction,...) 。由于 lapply 可用于列表,因此行数不必相等。

HTH

在您的函数中使用类似的内容:

    ggsave(filename,scale=1.5)

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:

    ggsave(filename,scale=1.5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文