ggplot graph with line above and below the median

51 Views Asked by At

I am trying to make a ggplot graph with two regression lines: One below and the other above the median to depict potential effect of age in the sample. So fa, I have tried: geom_hline (aes(yintercept = median(df$y) I do not get anything. I can also have two graphs, one with one line and the second one with the other line and combine them, but I haven't had success. Any help?

Yacila

geom_hline (aes(yintercept = median(df$y)

Expected a line above the mean

2

There are 2 best solutions below

0
Marco On BEST ANSWER

"The effect of age" sounds like age is on the x-axis. Here is a minimal example for two regression lines on either side of the median on the x-axis with tidyverse:

library(tidyverse)
data(mtcars)
mtcars %>% 
  ggplot(aes(x = mpg, y = qsec)) + geom_point() +
  geom_smooth(data = filter(mtcars, mpg <= median(mpg)), method = "lm", size = 1.2, se=F) +
  geom_smooth(data = filter(mtcars, mpg > median(mpg)), method = "lm", size = 1.2, se=F) +
  geom_vline(xintercept=median(mtcars$mpg))

enter image description here

0
Rui Barradas On

Credit

goes to Marco for pointing out that

"The effect of age" sounds like age is on the x-axis.


Answer

Include a group aesthetic on the condition of age being below or above the median. This will split the data into two groups.

df1 <- iris[1:2]
names(df1) <- c("y", "age")

library(ggplot2)

ggplot(df1, aes(age, y, group = age < median(age))) +
  geom_point() +
  geom_vline(xintercept = median(df1$age)) +
  geom_smooth(method = lm, formula = y ~ x, se = FALSE)

Created on 2023-03-12 with reprex v2.0.2