【问题标题】:how to use geom_label_repel with facet_wrap and geom_smooth?如何将 geom_label_repel 与 facet_wrap 和 geom_smooth 一起使用?
【发布时间】:2022-11-02 22:36:45
【问题描述】:

我的目标是在曲线的末端用 geom_smooth 标记每条曲线,但是当我使用 facet_wrap 时,标签没有正确放置并且并不总是出现。

这是我的例子:

library(ggrepel)
library(tidyverse)

df<-structure(list(month_day = structure(c(19124, 19124, 19132, 19132,19145, 19146, 19171, 19172, 19160, 19185, 19201, 19214, 19229,19244, 19110, 19259, 19273, 19104, 19116, 19130, 19131, 19144, 19166, 19179, 19193, 19208, 19229, 19243, 19256, 19271, 19131, 19145, 19145, 19171, 19171, 19157, 19160, 19185, 19185, 19201, 19201, 19214, 19214, 19229, 19229, 19244, 19244, 19110, 19110, 19259, 19259, 19273, 19273, 19104, 19104, 19116, 19144, 19130, 19166, 19179, 19208, 19229, 19243, 19256, 19256, 19271, 19271,19131, 19145, 19145, 19171, 19171, 19157, 19185, 19185, 19201, 19201, 19214, 19214, 19214, 19229, 19229, 19244, 19244, 19110,  19110, 19259, 19259, 19273, 19273, 19104, 19104, 19116, 19116,19130, 19144, 19166, 19179, 19179, 19193), class = "Date"), 
               Sensibilite = c(4,1, 0, 1, 0, 2, 3, 3, 2, 3, 4, 5, 5, 2, 0, 4, 4, 0, 1, 2, 3, 1,2, 2, 3, 3, 2, 2, 5, 4, 0, 2, 1, 3, 0, 2, 1, 5, 4, 5, 5, 5, 1, 4, 1, 3, 0, 0, 0, 5, 4, 5, 5, 0, 1, 1, 1, 1, 2, 1, 2, 2, 0, 4,3, 5, 4, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0,0, 1, 0, 1, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1),
               Nom = c("Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom1","Nom1", "Nom1", "Nom1", "Nom1", "Nom1", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2","Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2","Nom2", "Nom2", "Nom2", "Nom2","Nom2", "Nom2", "Nom2", "Nom2","Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom2", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3",  "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3",  "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3", "Nom3","Nom3", "Nom3"), 
               Annee = c("2021","2021","2021", "2021", "2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2022", "2022", "2022","2022", "2022","2022", "2022", "2022", "2022", "2022","2022", "2022","2022","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021","2021", "2021","2021","2021","2021","2021", "2021","2021","2021","2021","2022","2022","2022","2022","2022","2022","2022","2022","2022","2022","2022","2022", "2022","2022","2021","2021","2021","2021","2021","2021", "2021","2021","2021","2021","2021","2021","2021","2021","2021","2021", "2021","2021","2021","2021","2021","2021", "2021","2022","2022","2022", "2022","2022", "2022","2022","2022","2022", "2022"), 
               Lieu = c("Lieu1","Lieu1","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu1","Lieu1","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu1","Lieu1","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2","Lieu2" )), 
              row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))                                                                                    


ggplot(df, aes(month_day, Sensibilite, label = Nom, color = Nom)) +
  geom_smooth(se = FALSE) +
  geom_label_repel(aes(label = after_stat(ifelse(x %in% range(x), color, NA_character_))), 
                   stat = "smooth", 
                   max.overlaps = Inf,size = 2)+
  facet_wrap(Lieu~Annee,ncol = 2)+
  scale_y_continuous(limits = c(0,5))+
  scale_x_date(expand =  expansion(mult = 0.25),name = "Mois",date_breaks = "1 month",date_labels = "%b")

当我删除

  facet_wrap(Lieu~Annee,ncol = 2)+

标签是正确的(即使我更喜欢它们只在曲线的末端)

但是当我再次放置这条线时,一些标签会出现在曲线的开头,结尾,甚至根本没有。

有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: r ggplot2 facet-wrap ggrepel


    【解决方案1】:

    第一个问题是,在我们的ifelse 中,您没有考虑到colorPANEL 的分组(即刻面)。为此,我添加了一个自定义帮助函数,它使用一点dplyr 设置label 每个colorPANEL

    为了解决您的第二个问题,我使用两个 geom_label_repel 层分别在下端和上端添加标签。这样做允许使用将下端的标签向左轻推,在上端向右轻推。我还设置了将标签移动或排斥到"y" 方向的方向。最后,为了给标签腾出空间,我将 x 比例扩大了与用于轻推标签的量相同的量。

    library(ggrepel)
    library(tidyverse)
    
    helper_label <- function(x, color, PANEL, FUN = "min") {
      FUN <- switch(FUN,
        "min" = min,
        "max" = max
      )
      data.frame(x, color, PANEL) %>%
        group_by(PANEL, color) %>%
        mutate(label = ifelse(x %in% FUN(x), color, NA_character_)) %>%
        ungroup() %>%
        pull(label)
    }
    
    ggplot(df, aes(month_day, Sensibilite, label = Nom, color = Nom)) +
      geom_smooth(se = FALSE) +
      geom_label_repel(aes(label = after_stat(helper_label(x, color, PANEL, "min"))),
        stat = "smooth",
        max.overlaps = Inf, size = 2, na.rm = TRUE, direction = "y", nudge_x = -30
      ) +
      geom_label_repel(aes(label = after_stat(helper_label(x, color, PANEL, "max"))),
        stat = "smooth",
        max.overlaps = Inf, size = 2, na.rm = TRUE, direction = "y", nudge_x = 30
      ) +
      facet_wrap(Lieu ~ Annee, ncol = 2) +
      scale_x_date(
        expand = expansion(add = 30), name = "Mois",
        date_breaks = "1 month", date_labels = "%b"
      )
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2021-12-21
      • 1970-01-01
      • 2020-11-07
      • 2017-09-07
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      相关资源
      最近更新 更多