【问题标题】:Edit distance between the facet / strip and the plot编辑刻面/条带与绘图之间的距离
【发布时间】:2019-09-03 19:55:47
【问题描述】:

例如,

library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))

如何更改条带和主图之间的距离? (例如,在地带和主图之间创建一个间隙。)
但我不需要更改条带大小(与此 edit strip size ggplot2 不同)。

【问题讨论】:

标签: r ggplot2 facet-wrap facet-grid


【解决方案1】:

这个问题可以有多种解决方案。

geom_hline

一个 hacky 是在情节顶部添加一条线(可能是白色的,但这取决于您的主题)。我们可以使用geom_hline(或geom_vline,如果您的构面成行)来做到这一点。这会产生距离错觉。

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")

【讨论】:

    【解决方案2】:

    有一个更简单的解决方案

    theme(strip.placement = "outside")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      相关资源
      最近更新 更多