使用R生成可编辑的图表

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个回答

6

其他可编辑的格式:

请查看help(devices),了解其他可用的格式:这些包括svgpictexxfig等,这些格式具有不同程度的可编辑性。

请注意,PDF文件可以进行编辑,例如使用适用于Apple's 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创建可重复编辑的图表:

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

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

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()

4
感谢您的回答,我已经尝试过了,经过我的朋友Google的帮助,我找到了可以创建svg文件的Cairo包,然后我可以在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()

现在我只需要调整各种设置,使我的图表尽可能好看,然后再写入文件。

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接