绘制汇总分组分布的多项式回归线

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

假设我有这种数据,

value
观察结果按
size
的三个级别分组:

df <- data.frame(
  size = c(3,3,3,4,4,4,4,5,5,5,5,5),
  position = c(1,2,3,1,2,3,4,1,2,3,4,5),
  value = c(1,2,2.5,
            -0.5,0.5,-0.5,1,
            1.7,2,2.5,1.6,1.9)
)

我可以为每个

size
级别绘制多项式回归线:

library(tidyverse)
df %>%
  ggplot(aes(x = position, y = value, color = factor(size))) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE) +
  labs(x = "Index", y = "Value") +
  theme_minimal()

导致这个情节:

我需要的除了三个单独的行之外还有一行,尊重分组,总结了三个分布;即,显示

value
变量如何分布在三个水平上的回归线。 (如何)可以做到吗?

r ggplot2 non-linear-regression
© www.soinside.com 2019 - 2024. All rights reserved.