ggplot2
能够使用参数panel.margin
in 改变刻面图之间的边距opts
.这似乎改变了水平和垂直间距.有没有办法改变水平或垂直的间距而不改变另一个?
结果和期望结果的一个例子:
mtcars[, c("cyl", "am", "gear")] <- lapply(mtcars[, c("cyl", "am", "gear")], as.factor)
p <- ggplot(mtcars, aes(mpg, wt, group = cyl)) +
geom_line(aes(color=cyl)) +
geom_point(aes(shape=cyl)) +
facet_grid(gear ~ am) +
theme_bw()
p + opts(panel.margin = unit(1, "lines"))
Run Code Online (Sandbox Code Playgroud)
所以它目前看起来像:
我们怎样才能让它看起来更像:
小智 25
随着2015年7月9日时,panel.margin.x
并panel.margin.y
似乎已落实
p <- p + theme(panel.margin.x=unit(0.5, "lines") , panel.margin.y=unit(1,"lines"))
Run Code Online (Sandbox Code Playgroud)
截至2016年12月15日,'panel.spacing'和'panel.spacing.x'在r 3.3.2和ggplot2 2.2.0中实现
p <- p + theme(panel.spacing.x=unit(0.5, "lines"),panel.spacing.y=unit(1, "lines"))
Run Code Online (Sandbox Code Playgroud)
直到此功能可用的手动解决方案:
library(grid)
height <- 0.5 # Vertical spacing
aux <- 1e-5 # Auxiliary number to identify 'height' among other heights
width <- 0.1 # Desirable horizontal spacing
p <- p + theme(panel.margin = unit(height + aux, "lines"))
gtable <- ggplot_gtable(ggplot_build(p))
gtable$widths[sapply(gtable$widths, '[[', 1) == height + aux][[1]][[1]] <- width
grid.draw(gtable)
Run Code Online (Sandbox Code Playgroud)