如何在 ggplot2 R 中通过不同的因素绘制单个回归线但颜色点?

How to plot a single regression line but colour points by a different factor in ggplot2 R?

散点图按因子 z 进行颜色编码。默认情况下,ggplot2 还按因子绘制回归线。我想绘制一条通过数据的回归线。我该如何实现?

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("P1", "P2"), each  = 25)
df <- data.frame(x,y,z)

my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z), data = df) +
  geom_point() + scale_fill_manual(values=c("purple", "blue")) +
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),  parse = TRUE, size = 2.5, col = "black")+
  theme_classic()

如果我没理解错的话,您可以在 aes 中分配 group = 1 以仅绘制一条回归线。您可以使用以下代码:

library(tidyverse)
library(ggpmisc)
my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z, group = 1), data = df) +
  geom_point() + scale_fill_manual(values=c("purple", "blue")) +
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),  parse = TRUE, size = 2.5, col = "black")+
  theme_classic()

输出: