15

我希望顶部的灰色条更宽,例如,它的边缘离字母的顶部和底部稍远一点(strip.text - A、B、C 等)。我原以为 lineheight 会充当填充,但事实并非如此。

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

2 回答 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

于 2013-07-24T04:59:55.307 回答
1

使用ggplot22.2.0,您可以在不修改数据框的情况下应用 Ricardo 的技巧,方法是labeller

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))
于 2017-01-03T21:41:12.250 回答