【问题标题】:Rename Legend title on facet_wrap [duplicate]在 facet_wrap 上重命名图例标题 [重复]
【发布时间】:2021-12-11 13:30:06
【问题描述】:

我有 8 个图的分面包装,我想重命名图例,它目前默认为填充值 Depth_bins,但我希望能够更改它。我尝试了各种方法,包括以下使用 scale_color_discrete 但没有改变标题的方法。我假设这是因为它是 facet_wrap。

facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
  geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
  scale_x_datetime(date_labels = "%H:%M") +
  labs(x = "Start hour", y = "Density") + 
  facet_wrap(~ID, dir = "v", scales = "free_y") + 
  scale_color_discrete(name = "Depth Bins")
facet_8

非常感谢任何帮助!

【问题讨论】:

    标签: r ggplot2 legend facet-wrap


    【解决方案1】:

    我认为labs 函数应该像这里提到的那样工作:

    How to change legend title in ggplot

    所以,是这样的:

    facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
      geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
      scale_x_datetime(date_labels = "%H:%M") +
      labs(x = "Start hour", y = "Density") + 
      facet_wrap(~ID, dir = "v", scales = "free_y") + 
      labs(fill = "Your Title Here")
    facet_8
    

    【讨论】:

    • 这么简单,我尝试了 labs(legend = ) 和各种其他方法,但没想到要填满。现在有道理了。谢谢!
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2021-03-25
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多