gpt4 book ai didi

r - 在 ggplot 中为 geom_smoth 添加最大(峰值)值

转载 作者:行者123 更新时间:2023-12-05 04:34:44 24 4
gpt4 key购买 nike

我有一个 geom_smooth,它有一个 x 轴日期、y 轴 COVID 个案,然后是两个类别。我正在尝试绘制最大峰值。

# Reproducible data
library(tidyverse)
df <- tribble(~date, ~cases, ~category,
"2021/1/1", 100, "A",
"2021/1/1", 103, "B",
"2021/1/2", 108, "A",
"2021/1/2", 109, "B",
"2021/1/3", 102, "A",
"2021/1/3", 120, "B",
"2021/1/4", 150, "A",
"2021/1/4", 160, "B",
"2021/1/5", 120, "A",
"2021/1/5", 110, "B",
"2021/1/6", 115, "A",
"2021/1/6", 105, "B",)

# Plotting geom_smooth
df %>%
ggplot(df, mapping = aes(date, cases, group = category, color = category)) +
geom_smooth()

如何将最大峰值添加到 geom_smooth?理想情况下,我想要一个点和一个说明峰值情况的文本。

我尝试在 ggplot 代码之外找到峰值 - 但它返回一个不同的峰值,因为 geom_smooth 正在创建它自己的函数,而不仅仅是该类别的平均值。

下面的响应有效,但我想移动标签以使其更清晰,但 geom_text_repel 似乎仅指第一条曲线而不是两条曲线。有什么建议吗?

library(ggplot2)
library(tidyverse)
library(ggrepel)

# Fake data
ar =hist(rnorm(10000,1), breaks = 180, plot=F)$counts
br =hist(rnorm(11000,1), breaks = 180, plot=F)$counts

df <- rbind(
tibble(category="B", date = seq(as.Date("2021-01-01"),by=1, length.out=length(br)),value=br),
tibble(category="A", date = seq(as.Date("2021-01-01"),by=1, length.out=length(ar)),value=ar)
)
# create the smooth and retain rows with max of smooth, using slice_max
sm_max = df %>% group_by(category) %>%
mutate(smooth =predict(loess(value~as.numeric(date), span=.5))) %>%
slice_max(order_by = smooth)

# Plot, using the same smooth as above (default is loess, span set at set above)
df %>%
ggplot(df, mapping = aes(date, value, group = category, color = category)) +
geom_point() +
geom_smooth(span=.5, se=F) +
geom_point(data=sm_max, aes(y=smooth),color="black", size=5) +
geom_text_repel(data = sm_max, aes(label=paste0("Peak: ",round(smooth,1))), color="black")

geom_text_repel(data = sm_max_p3, aes(x = date,
y = smooth,
label = paste0(candidate, " Peak: ",round(smooth,1))

enter image description here

最佳答案

如果您只是想标记最大测量 值,您可以使用{gghighlight}仅显示和标记平滑曲线顶部的那个点。此外,您的 date 是一个 character,因此它是一个离散变量。因此,您的 geom_smooth() 只是一条点对点的线。在这里,我使用 mutate(date = lubridate::ymd(date)) 将其转换为连续变量。

library(tidyverse)
library(lubridate)
library(gghighlight)

df <- tribble(~date, ~cases, ~category,
"2021/1/1", 100, "A",
"2021/1/1", 103, "B",
"2021/1/2", 108, "A",
"2021/1/2", 109, "B",
"2021/1/3", 102, "A",
"2021/1/3", 120, "B",
"2021/1/4", 150, "A",
"2021/1/4", 160, "B",
"2021/1/5", 120, "A",
"2021/1/5", 110, "B",
"2021/1/6", 115, "A",
"2021/1/6", 105, "B",)

# Plotting geom_smooth
df %>%
mutate(date = ymd(date)) %>%
group_by(category) %>%
mutate(is_max = cases == max(cases)) %>%
ggplot(df, mapping = aes(date, cases, color = category)) +
geom_smooth() +
geom_point(size = 3) +
gghighlight(is_max,
n = 1,
unhighlighted_params = list(alpha = 0),
label_key = cases)

reprex package 创建于 2022-02-17 (v2.0.1)

关于r - 在 ggplot 中为 geom_smoth 添加最大(峰值)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71162252/

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