Multiple linear regression equation in ggplot2

ggplot2

#1

Hi,

I have a problem by putting multiple equation for multiple linear regression lines.
In fact, I have 3 series of samples completely different and I want to put them in the same scatter plot and I need to add 3 linear regression lines with their equations. So I used this script,

A <- (B <- ggplot(OM, aes(x= DOC , y= C1)) +
geom_point(size = 3)+ geom_smooth(method="lm",se=FALSE, formula = y ~ x)) +
geom_point(data = OM2, aes(x= DOC , y= C1), size = 3) + geom_smooth(data = OM2, aes(x= DOC , y= C1), method="lm", se=FALSE, formula = y ~ x) +
geom_point(data = OM3, aes(x= DOC , y= C1), size = 3) + geom_smooth(data = OM3, aes(x= DOC , y= C1), method="lm", se=FALSE, formula = y ~ x)+
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE)

But the problem with this script that I'm able to see the three regression lines (I used geom_smooth function) but I can't see the 3 equations when I use stat_poly_eq function. I see only one.

There is a solution to be able to add the three equation?

Thanks in advance
Amonda


#2

you'll get better help if you

  1. format your code with triple ` before and after so that it's readable
  2. Present code that can be run by those trying to help you (a reproducible example or "reprex")

I jumped through some hoops trying to figure out your code. you had a number of things that were not defined and I just made junk up. Try to provide those in the future.

The crux of your issue was that stat_poly_eq needs to be applied to each of the regressions as you build up the graph:

library(tidyverse)
library(ggpmisc)
#> For news about 'ggpmisc', please, see https://www.r4photobiology.info/
#> For on-line documentation see https://docs.r4photobiology.info/ggpmisc/
n <- 50

OM <- data.frame(DOC = 1:n,
                 C1 = 2 * (1:n + rnorm(n)))
OM2 <- data.frame(DOC = 1:n,
                  C1 = 3 * (1:n + rnorm(n)))
OM3 <- data.frame(DOC = 1:n,
                  C1 = 4 * (1:n + rnorm(n)))
my.formula <- y ~ x

A <- (
  ggplot() +
    geom_point(data = OM, aes(x = DOC , y = C1), size = 3) +
    geom_smooth(
      data = OM,
      aes(x = DOC , y = C1),
      method = "lm",
      se = FALSE,
      formula = y ~ x
    ) +
    stat_poly_eq(
      formula = my.formula,
      data = OM,
      aes(DOC, C1, label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
      parse = TRUE
    )
  +
    geom_point(data = OM2, aes(x = DOC , y = C1), size = 3) +
    geom_smooth(
      data = OM2,
      aes(x = DOC , y = C1),
      method = "lm",
      se = FALSE,
      formula = y ~ x
    ) +
    stat_poly_eq(
      formula = my.formula,
      data = OM2,
      aes(DOC, C1, label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
      parse = TRUE,
      label.y.npc = .9
    )
  +
    geom_point(data = OM3, aes(x = DOC , y = C1), size = 3) +
    geom_smooth(
      data = OM3,
      aes(x = DOC , y = C1),
      method = "lm",
      se = FALSE,
      formula = y ~ x
    ) +
    stat_poly_eq(
      formula = my.formula,
      data = OM3,
      aes(DOC, C1, label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
      parse = TRUE,
      label.y.npc = .85
    )
)
A

Created on 2019-02-05 by the reprex package (v0.2.1)


closed #3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.