我想将多条水平线拟合到一个图中,其中每条线代表数据中不同类别的平均值。
假设我有以下数据框
product <- c("A","A","B","B", "A","A", "B","B", "C","C", "D", "D", "C","C", "D", "D")
measurement <- c(120, 122, 42, 44, 119, 118, 45, 43, 280, 281, 502, 501, 279,278, 503, 504)
sample_data <- data.frame(product, measurement)
我想创建如下结果
ggplot(sample_data, aes(x=seq(length(sample_data$measurement)), y=measurement, colour= product)) +
geom_point() +
labs(x = "Data Points") +
geom_smooth(aes(group= product), formula = y~1, method="lm", col="blue", se=TRUE, size=.005)
我有两个问题:
任何帮助我将不胜感激。
您可以自己旋转来制作标签并验证计算:
library(dplyr)
sample_data_grouped <- sample_data |>
mutate(obs = row_number()) |>
summarize(obs_min = min(obs), obs_max = max(obs), mean = mean(measurement), .by = product)
ggplot(sample_data, aes(x=seq(length(sample_data$measurement)), y=measurement,
colour= product)) +
geom_point() +
labs(x = "Data Points") +
geom_segment(aes(x = obs_min, xend = obs_max, y = mean, yend = mean),
linetype = "dashed", data = sample_data_grouped) +
geom_text(aes(x = obs_min, y = mean, label = mean),
hjust = 0, vjust = -0.5, data = sample_data_grouped) +
geom_smooth(aes(group= product), formula = y~1, method="lm", col="blue", se=TRUE, size=.005)
如果你想在 ggplot“内部”完成这一切,你可以这样做:
library(geomtextpath)
ggplot(within(sample_data, `Data Points` <- seq(nrow(sample_data))),
aes(x = `Data Points`, y = measurement, colour = product)) +
geom_point() +
geom_textsegment(aes(y = ave(measurement, product),
x = ave(`Data Points`, product, FUN = min),
yend = ave(measurement, product),
xend = ave(`Data Points`, product, FUN = max),
label = after_stat(y)),
vjust = -0.2, textcolour = "black", linetype = 2)