我有一个 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))