【问题标题】:R ggplot, remove white margins in ggsave/ggplotR ggplot,删除 ggsave/ggplot 中的白边
【发布时间】:2016-05-22 15:06:14
【问题描述】:

如何去除 ggsave 中的白边?

我的问题和Remove white space (i.e., margins) ggplot2 in R一模一样。但是,那里的答案对我来说并不理想。我想给ggsave 一个heightweight,而不是反复试验固定但未知的纵横比,并希望我的情节(即标题顶部到x标签底部)自动扩展到那个没有白边的配置。

How can I remove the strange white margin around my .png (plotted with r, ggplot)? 提供了一种使边距透明的方法,但它们仍然存在,并且绘图小于我在保存的文件中设置的 heightwidth

【问题讨论】:

    标签: r ggplot2 ggsave


    【解决方案1】:

    我最终在ggsave 之后添加了这样的命令:

    system("/usr/local/bin/mogrify -trim -border 8 -bordercolor white output.png")
    

    -trim 删除现有边距,-border 8 -bordercolor white 在绘图周围添加一个小的 8px 边距。

    对于具有灰色背景的绘图,绘图边缘周围会留下一些白色像素,因此我使用 -shave 选项移除了一些额外的像素:

    system("/usr/local/bin/mogrify -trim -shave 4x4 output.png")
    

    【讨论】:

      【解决方案2】:

      在链接到this blog postthis answer 中,有一个解决方案也适用于不同的纵横比。您可以独立于操作系统裁剪硬盘上的图像:

      knitr::plot_crop()
      

      【讨论】:

        【解决方案3】:

        如果 pdf 和 pdfcrop 不是你的东西,例如你使用 png 徽标工作 - 那么请在此处查看我的答案:How to save a ggplot2 graphic with the proper aspect ratio?

        【讨论】:

          【解决方案4】:

          如果您使用的是 Unix 或 Mac OS,当各种边距选项不够修剪时,另一个选择是通过 R 调用系统命令的能力使用 Unix 中可用的 pdfcrop 命令:

          # after saving image, run pdfcrop 
          system2(command = "pdfcrop", 
                  args    = c("name_or_path_of_file_before_crop.pdf", 
                              "name_or_path_of_file_after_crop.pdf") 
                  )
          

          更多信息请见:https://robjhyndman.com/hyndsight/crop-r-figures/

          【讨论】:

          • 接受的答案不能满足我的需要,但 pdfcrop 可以。谢谢!
          【解决方案5】:

          Remove Plot Margins in ggplot2找到答案

          theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
          

          完成任务

          【讨论】:

          • 当纵横比改变例如theme(aspect.ratio=9/16) 这行不通。
          • 不确定为什么这个答案被接受。这只是删除了边距,使得轴标签、图表标题和面板都具有 0 边距。白色边框仍将使用 ggsave 打印。
          猜你喜欢
          • 1970-01-01
          • 2020-03-02
          • 2013-05-02
          • 2011-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-10
          • 1970-01-01
          相关资源
          最近更新 更多