R ggplot, remove the white fields in ggsave / ggplot

How to remove white fields in ggsave?

My question is exactly the same as Remove empty space (i.e. fields) ggplot2 in R. However, the answer there is not perfect for me. Instead of trial and error for a fixed but unknown aspect ratio, I would like to give ggsave a height and weight and I want my plot (i.e. the top heading below the x-mark) to automatically expand to this configuration without a white field.

How to remove a weird white marker around my .png (with a picture of r, ggplot)? makes it possible to make the margin transparent, but they are still there and the graph is smaller than height and width I have set to a saved file.

+9
source share
2 answers

Found answer from Delete graph fields in ggplot2

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

is work being done

+11
source

If you are using Unix or Mac OS, another option when the various field options are not trimmed enough is to use the pdfcrop command, available on Unix via R , 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 details see: https://robjhyndman.com/hyndsight/crop-r-figures/

0
source

Source: https://habr.com/ru/post/1242799/


All Articles