如何使用R保存和命名多个图 [英] How to save and name multiple plots with R

查看:96
本文介绍了如何使用R保存和命名多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由名为"L1"的函数"mcsv_r()"和一个生成映射的函数"gc()"生成的73个数据集的列表.使用"lapply",我可以创建我的73个图.我需要保存并命名所有人.我知道我可以使用RStudio一件一件地做. 但是我敢肯定,由于有了"jpeg()"和"dev.off"并将它们与循环混合在一起,我可以用几行代码来做到这一点.

I have a list of 73 data sets generated by the function "mcsv_r()" called "L1" and a function "gc()" that generates a map. Using "lapply" I can create my 73 plots. I need to save and name all of them. I know I can do it one by one with RStudio. But I am sure that thanks to "jpeg()" and "dev.off" and mixing them with a loop I can do it with a few lines of code.

out <- setwd("C:/")
dir(out)
mcsv_r(dir(out))

gc <- function(x){
  xlim <- c(-13.08, 8.68)
  ylim <- c(34.87, 49.50)
  map("world", lwd=0.05, xlim=xlim, ylim=ylim)
  map.axes()
  symbols(x$lon, x$lat, bg="#e2373f", fg="#ffffff", lwd=0.5, circles=rep(1, length(x$lon)), inches=0.05, add=TRUE)
  node <- x[x$node == 1, c("lon", "lat")]
  for (i in 2:nrow(x)) lines(gcIntermediate(node, x[i, c("lon", "lat")]), col="red", lwd=0.8)
}


lapply(L1, gc)

任何人都可以帮助我!?提前致谢.这是我的代码...

Anyone can help me!? Thanks in advance. This is my code...

推荐答案

如您在?jpeg中所读,您可以使用带有"C整数格式"的文件名,而jpeg将为每个图创建一个新文件,例如:

As you can read in ?jpeg you can use a filename with a "C integer format" and jpeg will create a new file for each plot, e.g.:

jpeg(filename="Rplot%03d.jpeg")
plot(1:10)  # will become Rplot001.jpeg
plot(11:20) # will become Rplot002.jpeg
dev.off()

在您的情况下,以下方法应该起作用:

In your case the following should work:

jpeg(filename="Rplot%03d.jpeg")
lapply(L1, gc)
dev.off()

这篇关于如何使用R保存和命名多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆