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

问题描述 投票:0回答:2

我想将多条水平线拟合到一个图中,其中每条线代表数据中不同类别的平均值。

假设我有以下数据框

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. 如何确保每条线代表每条线的平均值 产品?
  2. 如何显示线上每条平均线的值或 传说中的某个地方?

任何帮助我将不胜感激。

r ggplot2 plot mean scatter-plot
2个回答
2
投票

您可以自己旋转来制作标签并验证计算:

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)


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.