我的目標是在曲線的末端用 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)
標簽是正確的(即使我更喜歡它們只在曲線的末端)

但是一旦我再次放置這條線,一些標簽就會出現在曲線的開頭,結尾,甚至根本沒有。

有人知道我該如何解決這個問題嗎?
uj5u.com熱心網友回復:
第一個問題是ifelse你沒有考慮到分組color和PANEL(即刻面)。為此,我添加了一個自定義幫助函式,它使用一些dplyr設定labelpercolor和PANEL.
為了解決您的第二個問題,我使用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"
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527529.html
