【问题标题】:Create Editable plots from R从 R 创建可编辑的图
【发布时间】:2011-01-04 03:12:38
【问题描述】:

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

谢谢

保罗。

【问题讨论】:

    标签: graphics r ggplot2


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        使用 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】:

          其他可编辑格式:

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

          请注意,可以编辑 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 创建可重复、可编辑的绘图:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-13
            • 2015-09-27
            • 2019-10-16
            相关资源
            最近更新 更多