有没有办法在 facet 中增加 strip.text 条的高度?

16

我希望顶部的灰色条更宽些,即将其边缘与字母(strip.text - A、B、C等)的顶部和底部稍微拉开一点。我原以为行高会起到填充作用,但实际上并没有。

ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE)+
  facet_wrap(~ color) +
  theme(strip.text = element_text(lineheight=20)) 

增加文本大小本身不令人满意吗? - alexwhan
1
@alexwhan,增加文本大小也会增加带宽大小,但不会改变两者之间的比例。 - Ricardo Saporta
@RicardoSaporta,同意 - 我喜欢你的解决方案。 - alexwhan
2个回答

24

首先,修改级别以包含换行符:

levels(diamonds$color) <- paste0(" \n", levels(diamonds$color) , "\n ")

然后根据需要进行调整。例如:

P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
      xlim(0, 2) + stat_binhex(na.rm = TRUE)+
      facet_wrap(~ color)

P +  theme(strip.text = element_text(size=9, lineheight=0.5))

行高=0.5

P +  theme(strip.text = element_text(size=9, lineheight=3.0))

行高=3.0


1
回复有些晚了...谢谢,说实话这是一个不太优雅的解决方案哈哈,但肯定能完成任务。感谢你的帮助。 - nzcoops

1
使用ggplot2 2.2.0,您可以通过使用labeller来应用Ricardo的技巧,而无需修改数据框。
P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE)+
  facet_wrap(~ color, labeller = labeller(color=setNames(paste0("\n", levels(diamonds$color), "\n"), levels(diamonds$color))))

P +  theme(strip.text = element_text(size=9, lineheight=0.5))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接