从 R 创建可编辑绘图

发布于 2024-08-17 17:41:37 字数 496 浏览 10 评论 0 原文

我正在 R 中创建一系列绘图(我使用的是 ggplot2,但这不是必需的),我希望能够保存我的输出,以便我可以编辑它以供进一步使用,例如,我可能想移动关于图例,或调整颜色等。我看到 ggplot2 有一个保存命令,但似乎会生成 pdf 或位图,这两者都不是特别可编辑的

其他人如何做到这一点?有什么好主意吗?

这是一些生成示例图的示例代码;

library(ggplot2)
dataframe<-data.frame(fac=factor(c(1:4)),data1=rnorm(400,100,sd=15))
dataframe$data2<-dataframe$data1*c(0.25,0.5,0.75,1)
dataframe
testplot<-qplot(x=fac, y=data2,data=dataframe, colour=fac, geom=c("boxplot", "jitter"))
testplot

谢谢

保罗。

I'm creating a series of plots in R (I'm using ggplot2, but that's not essential) and I want to be able to save my output so I can then edit it for furthur use, For instance, I might want to move legends about, or adjust colours etc. I have seen that ggplot2 has a save command but that seems to produce pdf's or bitmaps, neither of which are particularly editable

How do other people do this ? Any good ideas ?

Here's some sample code to produce a sample plot;

library(ggplot2)
dataframe<-data.frame(fac=factor(c(1:4)),data1=rnorm(400,100,sd=15))
dataframe$data2<-dataframe$data1*c(0.25,0.5,0.75,1)
dataframe
testplot<-qplot(x=fac, y=data2,data=dataframe, colour=fac, geom=c("boxplot", "jitter"))
testplot

Thanks

Paul.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

挥剑断情 2024-08-24 17:41:37

其他可编辑格式:

查看 help(devices) 了解其他可用格式:包括 svgpictexxfig ,所有这些都可以或多或少地进行编辑。

请注意,可以编辑 PDF,例如使用适用于 Apple OSX 的 Omnigraffle 工具。

记录绘图数据的其他方法:

此外,您可以将 R 的命令记录到图形子系统以便稍后重复 - 看看 dev.copy

 Most devices (including all screen devices) have a display list
 which records all of the graphics operations that occur in the
 device. 'dev.copy' copies graphics contents by copying the display
 list from one device to another device.  Also, automatic redrawing
 of graphics contents following the resizing of a device depends on
 the contents of the display list.

使用 Rscript 创建可重复、可编辑的绘图:

I通常采用第三种策略,即将我的 R 会话复制到 Rscript 文件中,我可以重复运行该文件并调整绘图命令,直到它执行我想要的操作:

#!/usr/bin/Rscript
x = 1:10
pdf("myplot.pdf", height=0, width=0, paper="a4")
plot(x)
dev.off();

Other editable formats:

Take a look at help(devices) for other formats which are available: these include svg, pictex and xfig, all of which are editable to greater or lesser extents.

Note that PDFs can be edited, for instance using the Omnigraffle tool available for Apple's OSX.

Other ways to record plot data:

In addition, you can record R's commands to the graphics subsystem for repeating it later - take a look at dev.copy:

 Most devices (including all screen devices) have a display list
 which records all of the graphics operations that occur in the
 device. 'dev.copy' copies graphics contents by copying the display
 list from one device to another device.  Also, automatic redrawing
 of graphics contents following the resizing of a device depends on
 the contents of the display list.

Using Rscript to create a repeatable, editable plot:

I typically take a third strategy, which is to copy my R session into an Rscript file, which I can run repeatedly and tweak the plotting commands until it does what I want:

#!/usr/bin/Rscript
x = 1:10
pdf("myplot.pdf", height=0, width=0, paper="a4")
plot(x)
dev.off();
失而复得 2024-08-24 17:41:37

使用 ggplot 和lattice,您可以使用save 将绘图对象保存到磁盘,然后加载 并对其进行修改。例如:

save(testplot, file = "test-plot.rdata")

# Time passes and you start a new R session
load("test-plot.rdata")
testplot + opts(legend.position = "none")
testplot + geom_point()

With ggplot and lattice, you can use save to save the plot object to disk and then load it later and modify it. For example:

save(testplot, file = "test-plot.rdata")

# Time passes and you start a new R session
load("test-plot.rdata")
testplot + opts(legend.position = "none")
testplot + geom_point()
征﹌骨岁月お 2024-08-24 17:41:37

感谢您的回答,我已经解决了这个问题,在我的朋友 Google 的帮助下,我找到了 Cairo 包,它允许创建 svg 文件,然后我可以在 Inkscape

library(Cairo)
Cairo(600,600,file="testplot.svg",type="svg",bg="transparent",pointsize=8, units="px",dpi=400)
testplot
dev.off()
Cairo(1200,1200,file="testplot12200.png",type="png",bg="transparent",pointsize=12, units="px",dpi=200)
testplot
dev.off()

现在,我只需要在编写文件之前尝试各种设置,以使我的绘图尽可能好。

Thanks for the answers, I've played around with this, and after some help from my friend Google I found the Cairo package, which allows creation of svg files, I can then edit these in Inkscape.

library(Cairo)
Cairo(600,600,file="testplot.svg",type="svg",bg="transparent",pointsize=8, units="px",dpi=400)
testplot
dev.off()
Cairo(1200,1200,file="testplot12200.png",type="png",bg="transparent",pointsize=12, units="px",dpi=200)
testplot
dev.off()

Now I just have to play around with the various settings to get my plot as good as it can be before writing the file.

巨坚强 2024-08-24 17:41:37

在输出图上单击鼠标右键
复制为图元文件
然后将绘图保存到Word文档中(右键单击编辑图片以将绘图转换为Microsoft Office绘图对象)

right click the mouse on the output plot
Copy as metafile
then save plot into a word document (right click to edit picture to covert to the plot to Microsoft Office drawing Object)

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