改变ggplot2中的垂直和水平边距

时间:2016-01-20 19:54:14

标签: r ggplot2 margin facet

我用ggplot2制作了一个方面图。我想改变保证金。我发现 theme() panel.margin 参数可以用来改变边距大小。

library(grid)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(vs ~ am)
p <- p + facet_grid(vs ~ am)
p <- p + theme(panel.margin = unit(3, "lines"))
p

我想在x(水平)或y(垂直)方向上独立更改边距大小。有没有解决办法呢?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您要查找的参数已重命名为panel.spacing

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(vs ~ am)
p <- p + facet_grid(vs ~ am)
p <- p + theme(panel.spacing.x = unit(1, "lines"),
               panel.spacing.y = unit(3, "lines"))
p

enter image description here