gpt4 book ai didi

r - 如何在 r 中包含分段 geom_smooth 的标签?

转载 作者:行者123 更新时间:2023-12-05 03:35:59 26 4
gpt4 key购买 nike

我想在 r 中标记我的 geom_smooth,但标签更接近实际的点,而不是线。

数据:

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))

我的图表: enter image description here

此外,是否可以去掉指向平滑黄土曲线的直线?

最佳答案

实现所需结果的一个选项是在 ggrepel::geom_label_repel 中使用 stat="smooth"。这样做会将标签放在拟合点上而不是数据点上。但是,要完成这项工作,您必须使用例如 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")

关于r - 如何在 r 中包含分段 geom_smooth 的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69737016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com