在图周围裁剪出ggplot2空白 [英] Crop out ggplot2 whitespace around plot

查看:70
本文介绍了在图周围裁剪出ggplot2空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 coord_fixed()更改形状后,是否可以删除ggplot2图周围的空白?我希望将上方和下方的空白区域裁剪掉,以便仅保留绘图区域和轴标签.我在不保存的情况下将绘图输出渲染到R markdown文件中.

Is there a way to remove the white space surrounding a ggplot2 plot when the shape has been changed using coord_fixed()? I would like the white space above and below to be cropped away so that only the plotting area and axis labels remain. I am rendering the plot output in an R markdown file without saving.

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

下面的代码生成以下图:

The code below produces the following plot:

推荐答案

使用时:

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 less white space:

另一种选择是使用png或jpeg设备:

Another option could be to use the 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()

这篇关于在图周围裁剪出ggplot2空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆