9

我正在 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

谢谢

保罗。

4

4 回答 4

7

其他可编辑格式:

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

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

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

此外,您可以将 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 创建可重复、可编辑的绘图:

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

#!/usr/bin/Rscript
x = 1:10
pdf("myplot.pdf", height=0, width=0, paper="a4")
plot(x)
dev.off();
于 2010-01-12T10:56:03.447 回答
4

感谢您的回答,我玩过这个,在我朋友 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()

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

于 2010-01-12T13:57:36.597 回答
4

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

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()
于 2010-01-12T13:47:28.937 回答
0

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

于 2017-01-24T16:17:06.553 回答