如何在R中的ggplot中拟合多条平均(水平)线

qyzbxkaa  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(98)

我想在一个图中拟合多条水平线,其中每条线表示数据中不同类别的平均值。
假设我有以下 Dataframe

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)


的数据
我有两个问题:
1.我怎样才能确保每条线都代表每种产品的平均值?
1.我如何显示每条平均线在线上或图例中某处的值?
任何帮助我将不胜感激。

vmpqdwk3

vmpqdwk31#

您可以旋转自己的标签并验证计算结果:

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)

字符串


的数据

hl0ma9xz

hl0ma9xz2#

如果你想在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)

字符串


的数据

相关问题