【发布时间】: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