例如,
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))
如何更改条带和主图之间的距离? (例如,在条带和主图之间创建间隙。) 但我不需要改变条带尺寸(与此edit strip size ggplot2不同)。
这个问题可以有多种解决方案。
geom_hline
一个hacky就是在情节的顶部添加一条线(可能是白色,但这取决于你的主题)。我们可以使用geom_hline
(或geom_vline
,如果你的facet成行)。这造成了一种距离的幻觉。
library(ggplot2)
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(cols = vars(drv)) +
# Add white line on top (Inf) of the plot (ie, betweem plot and facet)
geom_hline(yintercept = Inf, color = "white", size = 4) +
labs(title = "geom_hline")
strip.background
另一个解决方案(由@atsyplenkov建议)是使用theme(strip.background = ...)
。在那里你可以指定边框的颜色。然而,这并不完美,因为它会从所有方向切割边界(可能有一种方法可以改善这一点)。
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(cols = vars(drv)) +
# Increase size of the border
theme(strip.background = element_rect(color = "white", size = 3)) +
labs(title = "strip.background")