【问题标题】:Is there a way to increase the height of the strip.text bar in a facet?有没有办法增加一个方面的 strip.text 栏的高度?
【发布时间】:2013-07-24 04:27:27
【问题描述】:

我希望顶部的灰色条更宽,例如,它的边缘离字母的顶部和底部稍远一点(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)) 

【问题讨论】:

  • 增大文本大小本身就不能令人满意?
  • @alexwhan,增加文字大小也会增加条带大小,但不会改变两者的比例
  • @RicardoSaporta,同意 - 我喜欢你的解决方案

标签: r ggplot2


【解决方案1】:

首先,修改关卡,使其包含换行符:

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

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

【讨论】:

  • 我的回复延迟了......谢谢,一个肮脏的解决方案让我们说实话哈哈,但肯定会完成工作。感谢您的帮助。
【解决方案2】:

使用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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多