Search code examples
rggplot2regressionscatter-plot

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


The scatterplot is colour-coded by factor z. By default, ggplot2 also pots the regression lines by factor. I want to plot a single regression line passing through the data. How do I achiece this?

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()

enter image description here


Solution

  • If I undertand you correctly, you can assign group = 1 in the aes to plot just one regression line. You can use the following code:

    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()
    

    Output:

    enter image description here