R ggplot, remove white margins in ggsave/ggplot

前端 未结 4 1094
孤街浪徒
孤街浪徒 2021-02-04 06:56

How can I remove the white margins in ggsave?

My question is exactly the same as Remove white space (i.e., margins) ggplot2 in R. However, the answer there isn\'t ideal

相关标签:
4条回答
  • 2021-02-04 07:17

    If you're using Unix or Mac OS, another option when the various margin options aren't trimming enough is to use the pdfcrop command available within Unix through R's ability to invoke system commands:

    # 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") 
            )
    

    For more, see: https://robjhyndman.com/hyndsight/crop-r-figures/

    0 讨论(0)
  • 2021-02-04 07:22

    In this answer linking to this blog post there is a solution which also works for different aspect ratios. You can crop the image on your hard drive, independently of OS:

    knitr::plot_crop()
    
    0 讨论(0)
  • 2021-02-04 07:25

    Found the answer from Remove Plot Margins in ggplot2

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

    does the job

    0 讨论(0)
  • 2021-02-04 07:35

    If pdf and pdfcrop aren't your thing, for example you work in png with a png logo - then see my answer here: How to save a ggplot2 graphic with the proper aspect ratio?

    0 讨论(0)
提交回复
热议问题