【问题标题】:How to label only some levels with guide_coloursteps?如何使用 guide_coloursteps 仅标记某些级别?
【发布时间】:2021-05-07 14:03:03
【问题描述】:

这是我在 Stackoverflow 的第一个问题,所以我希望风格是正确的。

我有一个像这个例子一样的等高线图:

ggplot(faithfuld, aes(x = waiting, y = eruptions, z = 100 * density)) +
  geom_contour_filled(breaks = seq(-4, 4, length.out = 25)) + 
  scale_fill_viridis_d(guide = guide_coloursteps(barwidth = unit(7,"cm"), show.limits = T)) +
  theme( legend.position = "bottom")

不幸的是,只能标记整个轴或删除所有标签 (label = FALSE)。我想为极值加上标签,也许在中间一个,一个在中间和边界之间。

有什么解决办法吗?

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    有两种解决方法,我们可以使用旋转标签,

    theme(legend.position = "bottom",
          legend.text = element_text(angle = 90))
    

    或者,我们可以手动提供标签,

    scale_fill_viridis_d(guide = guide_coloursteps(barwidth = unit(7,"cm"), show.limits = F),
                         labels = c('0','','','','','2','','','','','4')) +
    

    它并不优雅,但 labels 参数需要 label 对应每个 category

    【讨论】:

    • 非常感谢。这真的解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多