我想geom_smooth在 r 中標記 my ,但標簽更接近實際點,而不是線。
資料:
df <- structure(list(t = c(45, 45, 45, 45, 45, 46, 46, 46, 46, 46,
47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 50,
50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53,
53, 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56,
56, 56, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 59, 59, 59, 59,
59, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62,
63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65),
x = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), name = c("P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4", "P1", "P2", "P2 ", "P3", "P4", "P1", "P2",
"P2 ", "P3", "P4"), value = c(48L, 132L, 111L, 115L, 2L,
58L, 126L, 82L, 74L, 0L, 45L, 119L, 78L, 87L, 0L, 56L, 106L,
105L, 88L, 1L, 52L, 78L, 91L, 107L, 1L, 35L, 96L, 86L, 98L,
1L, 61L, 118L, 90L, 108L, 2L, 45L, 114L, 93L, 98L, 2L, 55L,
108L, 78L, 76L, 7L, 44L, 97L, 94L, 96L, 0L, 40L, 111L, 93L,
88L, 1L, 43L, 78L, 66L, 113L, 2L, 20L, 57L, 84L, 41L, 0L,
17L, 51L, 81L, 34L, 0L, 40L, 55L, 64L, 32L, 0L, 25L, 67L,
71L, 37L, 0L, 16L, 67L, 60L, 57L, 0L, 23L, 46L, 62L, 47L,
1L, 34L, 75L, 68L, 39L, 0L, 34L, 60L, 85L, 24L, 0L, 20L,
58L, 63L, 37L, 1L)), row.names = c(NA, -105L), class = c("tbl_df",
"tbl", "data.frame"))
我的代碼:
df %>%
group_by(name) %>%
# New column to label the first and last values
mutate(label =
case_when(
t == min(t) | t == max(t) ~ name,
TRUE ~ NA_character_)) %>%
ggplot(aes(x = t, y = value, color = name, group = interaction(x, name), shape = name))
geom_smooth(se = F)
geom_vline(xintercept = 57, linetype = "dashed", lwd = 0.3)
scale_color_discrete(guide = 'none')
geom_label_repel(aes(label = label))
我的圖表:

另外,是否有可能擺脫指向平滑黃土曲線的線條?
uj5u.com熱心網友回復:
實作所需結果的一種選擇是stat="smooth"在ggrepel::geom_label_repel. 這樣做會將標簽放在擬合點而不是資料點上。但是,要完成這項作業,您必須geom_label_repel使用例如
after_stat(ifelse(x %in% range(x), color, NA_character_)):
library(ggplot2)
library(ggrepel)
ggplot(df, aes(x = t, y = value, color = name, group = interaction(x, name), shape = name))
geom_smooth(se = F)
geom_vline(xintercept = 57, linetype = "dashed", lwd = 0.3)
scale_color_discrete(guide = "none")
ggrepel::geom_label_repel(aes(label = after_stat(ifelse(x %in% range(x), color, NA_character_))), stat = "smooth")

編輯可能有不同的路線將標簽放在資料范圍的(下)上端的左側(右側)上。我的方法可能并不優雅。但是最近我不得不在端點處準備了很多帶有標簽的圖表之后,我決定通過兩個文本層來做這件事在我看來是實作這一目標的最簡單方法,因為它允許我們輕推將標簽向左移動或對。為了完成這項作業,我增加了 x 比例的擴展,以便為標簽騰出空間。此外,我洗掉了編輯和設定中的段,.direction = "y"以便僅在 y 方向排斥標簽。
library(ggplot2)
library(ggrepel)
ggplot(df, aes(x = t, y = value, color = name, group = interaction(x, name), shape = name))
geom_smooth(se = F)
geom_vline(xintercept = 57, linetype = "dashed", lwd = 0.3)
scale_color_discrete(guide = "none")
scale_x_continuous(expand = c(.1, .1))
ggrepel::geom_label_repel(aes(label = after_stat(ifelse(x %in% range(x)[1], color, NA_character_))),
stat = "smooth",
nudge_x = -1,
min.segment.length = Inf,
direction = "y")
ggrepel::geom_label_repel(aes(label = after_stat(ifelse(x %in% range(x)[2], color, NA_character_))),
stat = "smooth",
nudge_x = 1,
min.segment.length = Inf,
direction = "y")

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