Trim spaces ggplot2 around the plot

Is there a way to remove the empty space around the ggplot2 graph when the form was resized using coord_fixed() ? I would like the white space at the top and bottom to be cropped so that only the marks of the print area and axis remain. I draw the graph in the R markup file without saving.

  p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() p + coord_fixed(ratio = 1) 

The code below shows the following graph:

plot with space

+5
source share
1 answer

Using:

 ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(ratio = 1) + ggsave('plot.jpg', width = 6, height = 1.5, dpi = 300) 

You get a plot with fewer spaces:

enter image description here

Another option would be to use a png or jpeg device:

 p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + coord_fixed(ratio = 1) jpeg('plot.jpg', width = 600, height = 150) p dev.off() 
+3
source

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


All Articles