在绘图周围裁剪出ggplot2空格

时间:2022-08-27 10:42:08

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.

有没有办法在使用coord_fixed()更改形状时删除ggplot2图周围的空白区域?我希望裁剪上方和下方的空白区域,以便仅保留绘图区域和轴标签。我在没有保存的情况下在R markdown文件中渲染绘图输出。

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

The code below produces the following plot:

下面的代码生成以下图表:

在绘图周围裁剪出ggplot2空格

1 个解决方案

#1


3  

When you use:

当你使用:

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:

你会得到一个空白较少的情节:

在绘图周围裁剪出ggplot2空格

Another option could be to use the png or jpeg device:

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

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

jpeg('plot.jpg', width = 600, height = 150)
p
dev.off()

#1


3  

When you use:

当你使用:

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:

你会得到一个空白较少的情节:

在绘图周围裁剪出ggplot2空格

Another option could be to use the png or jpeg device:

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

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

jpeg('plot.jpg', width = 600, height = 150)
p
dev.off()