首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R图形:输出到几种文件格式

R图形:输出到几种文件格式
EN

Stack Overflow用户
提问于 2020-01-06 00:21:53
回答 1查看 175关注 0票数 5

在许多脚本中,我首先在屏幕上开发一个图形,然后需要将其保存为具有特定高度/宽度/分辨率的几种文件格式。用png()pdf()svg(),要打开一个设备,然后用dev.off()关闭它,我不得不将所有打开的设备调用放到我的脚本中,并将它们注释掉&每次只运行一个设备的代码。

我确实知道,对于ggplot图形,ggsave()使这更容易。对于基-R,格子图形,我能做些什么来简化这个过程吗?

一个例子是:

代码语言:javascript
复制
png(filename="myplot.png", width=6, height=5, res=300, units="in")
# svg(filename="myplot.svg", width=6, height=5)
# pdf(filename="myplot.pdf", width=6, height=5)

op <- par()  # set graphics parameters
plot()       # do the plot
par(op)
dev.off() 
EN

回答 1

Stack Overflow用户

发布于 2020-01-06 03:27:01

图形设备是grDevices包的一部分。使用多个开放设备的文档可能值得一读。据我所知,打开设备的循环数组被存储,但只有当前设备处于活动状态。因此,打开所有想要的设备,然后在它们上循环使用dev.list()可能是最好的选择。

代码语言:javascript
复制
# data for sample plot
x <- 1:5
y <- 5:1

# open devices
svg(filename="myplot.svg", width=6, height=5)
png(filename="myplot.png", width=6, height=5, res=300, units="in")
pdf()

# devices assigned an index that can be used to call them
dev.list()
svg png pdf 
  2   3   4 

# loop through devices, not sure how to do this without calling plot() each time
# only dev.cur turned off and dev.next becomes dev.cur
for(d in dev.list()){plot(x,y); dev.off()} 

# check that graphics device has returned to default null device
dev.cur()
null device 
      1 
dev.list()
NULL

file.exists("myplot.svg")
[1] TRUE
file.exists("myplot.png")
[1] TRUE
file.exists("Rplots.pdf") # default name since none specified in creating pdf device
[1] TRUE

您可以使用的文档中有很多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59605378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档