UseR10085
UseR10085

Reputation: 8176

Fitting linear regression lines for maximum and minimum of y for a particular value of x in R

I want to fit linear regression trend lines for maximum and minimum of y for a particular value of x. I am using the following code

library(tidyverse)
ggplot(data = df, aes(x = x, y = y)) + geom_point()

To find out the maximum and minimum values of y for a particular value of x, I am using the following code

df %>% 
  group_by(x) %>% 
  summarise(maximum = max(y),
            minimum = min(y))

Now how can I have the following plot with corresponding regression equation and R^2

enter image description here

Data

df = structure(list(x = c(0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 0.07, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 
0.09, 0.09, 0.09, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 
0.11, 0.11, 0.11, 0.11, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 
0.17, 0.17, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 
0.21, 0.21, 0.21, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 
0.23, 0.23, 0.23, 0.23, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 
0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 
0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 
0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.27, 0.29, 0.29, 0.29, 
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 
0.29, 0.29, 0.29, 0.29, 0.29, 0.3, 0.3, 0.3, 0.3, 0.31, 0.31, 
0.31, 0.31), y = c(25.07, 25.41, 26.08, 25.3, 26.95, 26.85, 24.69, 
26.94, 26.49, 27.1, 26.14, 27.25, 28.5, 25.38, 26.07, 27.92, 
24.6, 29.24, 25.96, 25.91, 25.66, 26.8, 26.84, 25.37, 26.41, 
26.34, 25.54, 28.53, 29.08, 27.31, 26.07, 26.57, 26.52, 26.39, 
25.85, 29.28, 26.31, 27.12, 25.15, 26.24, 26.21, 25.68, 26.54, 
28.72, 25.73, 26.76, 25.89, 26.77, 27.29, 26.34, 26.07, 26.54, 
25.95, 29, 26.16, 25.23, 25.17, 27.07, 27.93, 26.12, 27.2, 26.89, 
26.7, 25.96, 26.61, 26.37, 27.95, 28.98, 25.76, 25.09, 25.57, 
25.94, 27.55, 25.1, 26.73, 27.24, 26.47, 25.44, 25.28, 27.65, 
26.52, 27.42, 25.57, 25.46, 26.9, 27.26, 26.49, 25.39, 27, 26.22, 
26.82, 25.72, 26.01, 28.61, 26.4, 27.95, 27.51, 28.26, 25.5, 
25.99, 24.69, 24.71, 26.25, 25.65, 26.84, 26.27, 27.25, 27.93, 
25.47, 27.29, 26.69, 24.92, 28.24, 25.37, 25.19, 25.52, 26.11, 
25.84, 26.19, 26.88, 25.8, 28.66, 26.29, 28.75, 26.64, 28.22, 
26.11, 26.95, 26.19, 26.18, 28.2, 29.21, 26.31, 27.06, 25.71, 
25.92, 26, 25.32, 27.95, 25.59, 26.09, 25.47, 27.01, 26.37, 26.49, 
25.46, 25.94, 26.8, 25.76, 25.88, 26.27, 28.79, 26.68, 25.04, 
29.48, 26.73, 26.24, 26.72, 25.58, 25.48, 29.57, 26.44, 27.47, 
26.64, 25.61, 26.53, 26.26, 26.24, 25.9, 24.72, 25.35, 25.19, 
25.43, 26.94, 25.56, 26.47, 29.26, 25.93, 27.03, 26.04, 25.78, 
24.97, 25.52, 25.29, 26.46, 26.13, 26.22, 25.93, 26.48, 25, 25.36, 
25.76, 26.27, 26.44, 29.59, 25.84, 26.53, 29.47, 26.25, 25.85, 
27.48, 27.83, 24.66, 26.03, 25.44, 27.62, 24.93, 25.71, 26.47, 
24.74, 28.73, 27.31, 26.26, 26.36, 25.41, 28.74, 26.6, 26.54, 
25.76, 26.2, 27.67, 25.13, 27.53, 25.28, 28.25, 26.41, 24.92, 
27.85, 28.75, 27.02, 25.99, 29.48, 28.9, 26.45, 29.36, 26.05, 
24.99, 26.38, 25.63, 25.39, 28.12, 25.29, 26.61, 28.36, 26.38, 
25.47, 26.66, 26.3, 25.03, 26.58, 26.22, 25.86, 26.71, 26.32, 
27.1, 24.44, 27.18, 25.27, 24.97, 26.01, 26.79, 24.75, 27.22, 
26.62, 28.4, 26.75, 26.25, 27.26, 25.74, 26.54, 26.21, 26.85, 
26.9, 28.89, 24.82, 25.1, 25.51, 27.16, 28.76, 26.03, 26.23, 
25.23, 24.6, 24.95, 26.3, 25.61, 28.81, 26.82, 27.79, 25.59, 
25.78, 26.33, 26.19, 25.22, 26.68, 27.4, 26.59, 25.4, 24.63, 
29.42, 26.28, 24.83, 26, 26.05, 26.28, 25.78, 26.92, 27.42, 26.65, 
26.47, 27.53, 26.88, 26.93, 25.67, 27.7, 25.48, 25.61, 25.58, 
25.13, 27.68, 26.64, 26.87, 25.17, 28.55, 27.07, 24.5, 28.05, 
25.63, 27.62, 26.39, 24.58, 24.93, 26.36, 24.98, 27.78, 27.39, 
25.14, 24.63, 24.83, 26.59, 27.97, 25.23, 27.15, 25.69, 26.44, 
28.37, 27, 26.08, 25.9, 24.45, 25.13, 25.25, 26.32, 26.08, 25.03, 
26.84, 25.4, 25.59, 26.19, 25.3, 26.31, 26, 26.72, 29.05, 27.35, 
25.41, 26.49, 27.48, 27.36, 24.99, 26.89, 25.39, 26.98, 25.39, 
26.55, 24.61, 26.57, 25.38, 24.58, 25.76, 26.2, 24.82, 25.41, 
24.93, 29.98, 26.97, 27.26, 25.44, 25.37, 27.02, 26.52, 25.61, 
25.61, 27.98, 26.67, 26.18, 26.05, 26.16, 25.7, 24.65, 25.85, 
26.35, 27.73, 27.3, 28.1, 26.27, 26.32, 24.66, 29.12, 25.98, 
25.99, 25.59, 27.15, 24.46, 26.39, 28.18, 25.28, 25.56, 28.61, 
24.19, 25.87, 26.49, 25.02, 25.17, 25.21, 25.54, 25, 26.39, 24.11, 
26.37, 26.33, 26.8, 25.96, 25.06, 26.61, 29.23, 26.79, 26.26, 
26.97, 26.23, 25.67, 25.66, 27.04, 25.17, 27.5, 26.26, 26.33, 
26.97, 27.09, 29.59, 26.08, 26.1, 25.51, 25.38, 26.2, 24.18, 
25.1, 28.04, 26.04, 26.51, 26.1, 28.68, 24.97, 27.75, 26.97, 
24.71, 25.35, 24.36, 25.26, 24.62, 26.73, 25.52, 26.66, 28.15, 
26.24, 25.66, 26.29, 24.8, 25.34, 26.41, 26.12, 24.9, 26.99, 
26.76, 26.06, 26.47, 27.56, 26.8, 25.35, 27.91, 30.09, 28.37, 
26.66, 24.79, 26.24, 27.25, 26.78, 27.71, 29.42, 27.04, 26.19, 
25.14, 25.48, 25.86, 25.74, 25.35, 26.39, 25.68, 24.09, 25.73, 
26.53, 26.31, 24.83, 26.5, 27.77, 24.99, 24.9, 26.3, 24.87, 24.9, 
24.77, 26.45, 25.4, 26.87, 24.74, 25.81, 27.29, 24.83, 26.84, 
26.16, 27.79, 24.66, 25.54, 28.88, 28.08, 26.31, 26.13, 26.45, 
27.64, 28.25, 25.95, 25.45, 24.33, 26.25, 24.95, 25.72, 24.78, 
27.33, 27.26, 26.46, 25.19, 25.37, 25.49, 25.9, 25.7, 28.46, 
27.07, 26.32, 26.07, 25.24, 24.97, 24.98, 26.31, 28.19, 26, 26.56, 
24.95, 29.22, 25.95, 25.17, 25.35, 26.48, 26.82, 25.39, 25.69, 
28.38, 26.08, 27.1, 24.33, 24.13, 28.34, 26.03, 25.57, 27.45, 
24.53, 26.33, 24.99, 25.96, 29.34, 25.03, 25.39, 25.33, 25.85, 
25.33, 25.21, 25.67, 26.03, 26.55, 27.1, 27.56, 25.04, 24.75, 
25.24, 26.54, 27.77, 26.65, 27.2, 24.68, 24.97, 28.13, 26.73, 
27.64, 26.03, 24.58, 28.3, 25.95, 26.23, 24.89, 28.47, 26.46, 
24.76, 26.92, 24.99, 24.48, 25.15, 25.46, 24.56, 25.69, 24.73, 
25.27, 25.85, 26.39, 29.7, 28.71, 27.84, 26.23, 27.37, 25.79, 
26.56, 26.28, 27.08, 26.26, 27.04, 25.17, 28.08, 25.91, 24.55, 
24.37, 24.24, 25.34, 25.76, 25.4, 24.53, 26.77, 25.73, 25.22, 
26.04, 26.41, 26.72, 24.57, 24.95, 25.12, 29.65, 25.43, 27.92, 
25.48, 25.99, 24.68, 25.01, 25.19, 25.05, 24.84, 27.53, 27.3, 
25.14, 25.26, 26.25, 24.16, 25.17, 29.84, 27.84, 26.37, 26.17, 
24.89, 28.2, 24.96, 26.18, 27.66, 26.67, 25.62, 25.51, 24.62, 
24.07, 28.08, 26.57, 24.94, 25.93, 25.64, 28.85, 25.43, 29.82, 
25.36, 25.79, 25.87, 26.43, 25.67, 27.15, 25.58, 25.76, 25.61, 
25.48, 25.6, 27.17, 25.12, 25.84, 24.64, 26.68, 25.85, 25.01, 
27.78, 26.41, 25.64, 27.38, 25.62, 27.36, 24.26, 27.07, 25.28, 
28.9, 27.72, 28.67, 27.81, 26, 25.74, 25.53, 25.36, 25.27, 28.96, 
25.18, 26.69, 26.17, 27.21, 26.29, 24.79, 25.43, 26.28, 25.9, 
29.1, 29.92, 25.84, 27.45, 27.68, 27.06, 28.01, 25.04, 25.85, 
24.77, 25.24, 26.68, 26.6, 24.98, 26.26, 25.26, 24.6, 28.85, 
27.91, 25.04, 24.6, 26.89, 29.48, 25.89, 27.53, 26.18, 26.46, 
25.52, 24.98, 24.5, 25.43, 27.62, 25.83, 26.26, 25.98, 25.68, 
27.79, 29.98, 25.93, 24.92, 24.98, 25.2, 26.34, 24.69, 25.11, 
26.83, 25.41, 25.12, 26.17, 25.22, 26.12, 27.73, 26.1, 26.64, 
26.13, 25.8, 27.12, 25.67, 25.61, 25.24, 25.73, 24.54, 25.17, 
26.78, 25.68, 27.67, 24.52, 25.79, 25.01, 25.8, 26.55, 26.01, 
24.86, 25.09, 24.34, 25.38, 25.41, 24.88, 25.35, 26.3, 26.2, 
24.89, 24.36, 26.35, 27.07, 26.59, 24.49, 25.85, 26.24, 27.06, 
24.31, 26.31, 24.79, 25.83, 26.51, 25.58, 26.06, 26.01, 26.05, 
26.08, 26.31, 26.62, 27.17, 24.63, 25.25, 25.11, 28.05, 25.12, 
27.64, 24.65, 26.26, 25.62, 27.77, 27.78, 25.75, 25.1, 24.61, 
25.88, 25, 25.18, 25.19, 27.09, 24.97, 25.09, 25.83, 24.42, 27.14, 
24.53, 25.15, 29.05, 26.91, 26.24, 24.54, 25.43, 24.98, 26.1, 
26.77, 25.39, 24.47, 25.97, 24.71, 25.64, 25, 24.95, 26.77, 25.05, 
25.99, 25.13, 25.49, 26.85, 27.99, 25.58, 27.1, 24.86, 25.79, 
24.91, 25.14, 25.94, 28.91, 26.94, 28.68, 27.05, 26.56, 25.08, 
26.04, 25.06, 25.4, 28.18, 26.55, 24.92, 25.11, 25.18, 25.92, 
26.73, 25.81, 24.63, 25.22, 26.75, 24.93, 27.33, 25.03, 27.62, 
24.57, 26.61, 25.23, 27.41, 28.26, 25.15, 25.05, 27.96, 26.35, 
26.65, 26.21, 24.59, 24.45, 28.26, 24.83, 25.86, 26.46, 24.89, 
24.92, 25.16, 28.93, 26.57, 27.26, 27.33, 25.6, 28.13, 25.49, 
26.93, 26.14, 25.32, 25.18, 25.41, 25.12, 29.18, 27.16, 25.91, 
26.28, 26.68, 25.4, 25.18, 24.91, 27.04, 25.98, 26.75, 25.45, 
25.1, 24.48, 25.45, 25.47, 28.67, 26.48, 24.95, 24.96, 26.56, 
25.71, 25.17, 25.28, 25.62, 24.94, 26.83, 26.85, 25.72, 26.97, 
25.43, 25.06, 25.71, 29.29, 24.76, 24.74, 24.92, 25.47, 24.73, 
24.66, 27.16, 27.38, 26.05, 25.08, 25.25, 24.93, 24.8, 27.16, 
26.05, 26.3, 26.24, 24.67, 27.68, 25.33, 26.6, 25.11, 25.8, 26.09, 
25, 25.69, 28.47, 26.03, 26.54, 26.14, 24.36, 24.93, 24.84, 27.97, 
27.07, 24.65, 24.52, 25.75, 25.96, 25.02, 24.93, 26.95, 26.47, 
28.13, 28.46, 25.61, 25.49, 24.59, 24.98, 24.22, 24.48, 25.04, 
28.85, 25.05, 25.15, 28.31, 25.3, 25.04, 25.05, 25.12, 25.14, 
24.84, 25.32, 26.85, 26.56, 25.02, 25.2, 24.52, 27.45, 25.32, 
25.81, 25.08, 25.25, 25.31, 24.53, 26.31, 28.57, 26.28, 25.94, 
25.1, 26.83, 26.3, 27.26, 25.19, 25.02, 26.39, 26.91, 25.07, 
24.82, 26.25, 24.42, 26.82, 25.3, 25.02, 29.23, 24.77, 26.03, 
25.47, 27.41, 26.08, 25.68, 25.33, 27.77, 27.3, 25.14, 24.87, 
25.11, 27.38, 26.44, 25.33, 26.15, 24.58, 25.91, 25.13, 28.4, 
24.98, 25.41, 28.53, 25.1, 25.05, 24.91, 24.49, 26.04, 25.68, 
26.78, 26.85, 25.52, 24.69, 26.25, 27.75, 24.99, 27.75, 24.62, 
24.6, 26.68, 26.42, 24.39, 25.37, 29.87, 28.33, 26.35, 25.36, 
25.16, 27.47, 26.62, 24.72, 25.81, 24.92, 26.78, 25.27, 25.18, 
27.6, 24.71, 25.74, 24.46, 24.98, 24.93, 25.68, 25.15, 24.96, 
25.98, 25.1, 26.16, 25.35, 25.15, 25.53, 26.25, 26.85, 25.66, 
25.74, 26.03, 25.55, 25.56, 24.46, 24.88, 25.04, 24.64, 24.97, 
26.04, 27.14, 24.43, 27.8, 27.43, 25.31, 26.36, 25.38, 25.49, 
25.49, 24.67, 27.26, 25.05, 25.51, 25.16, 26.02, 24.87, 25.18, 
24.98, 25.13, 25.62, 25.29, 26.67, 24.91, 25.06, 25.65, 25.09, 
24.72, 27.21, 25.7, 25.92, 24.89, 25.02, 26.66, 27.21, 26.8, 
25.89, 26.68, 25.91, 24.93, 24.38, 25.08, 25.25, 25.55, 27.11, 
25.57, 25.33, 24.95, 24.74, 25.03, 25.39, 25.55, 24.71, 26.94, 
26.84, 24.56, 28.64, 25.14, 26, 27.58, 27.59, 26.5, 27.26, 26.24, 
26.71, 25.03, 26.69, 24.42, 26.2, 24.83, 26.41, 25.28, 26.42, 
25.29, 25.04, 24.55, 24.52, 25.66, 25.06, 27.86, 26.58, 26.05, 
24.78, 25.18, 25.58, 27.45, 25.25, 24.95, 24.62, 24.6, 25.4, 
25.07, 25.24, 24.96, 26.49, 26.34, 25.88, 25.63, 24.84, 24.96, 
24.79, 25.3, 25.59, 25.83, 26, 26.83, 25.75, 27.09, 25.02, 25.29, 
26.19, 24.68, 24.45, 25.22, 25.43, 27.69, 24.57, 25.15, 27.44, 
26.55, 25.09, 28, 26.16, 24.93, 24.98, 25.34, 28.66, 27.59, 26.33, 
24.41, 25.22, 24.87, 24.56, 25.3, 25.01, 25.44, 24.94, 26.52, 
25.68, 24.71, 26.83, 24.93, 24.35, 26.48, 25.25, 25.79, 26.16, 
27.29, 24.71, 25.37, 26.77, 25.52, 25, 24.63, 25.34, 24.67, 25.23, 
25.17, 24.93, 27.37, 25.94, 25.75, 25.1, 25.51, 26.22, 26.6, 
25.03, 25.01, 26.51, 24.85, 24.94, 25.23, 29.16, 27.08, 25.4, 
24.86, 24.64, 26.44, 24.26, 24.87, 25.54, 25.09, 26.84, 25.24, 
24.89, 26.6, 25.86, 26.75, 24.8, 26.29, 26.09, 27.93, 25.42, 
25.17, 27.11, 27.4, 27.12, 25.33, 25.15, 25.34, 25.14, 24.63, 
25.97, 24.75, 25.76, 26.9, 26.62, 25.24, 28.55, 26.48, 25.15, 
25.92, 26.12, 24.97, 25.79, 25.91, 25.09, 25.67, 24.75, 28.64, 
25.43, 25.12, 25.13, 25, 24.88, 27.3, 25.39, 25.4, 25, 24.81, 
24.53, 24.97, 28, 24.29, 24.82, 25.24, 26.76, 25.51, 26.54, 25.17, 
25.24, 25.79, 26.43, 26.61, 25.43, 26.87, 25.29, 26.79, 28.5, 
24.92, 26.59, 25.43, 26.19, 24.79, 24.77, 26.85, 25.21, 25.34, 
26.31, 25.17, 26.13, 25.89, 26.4, 25.01, 24.66, 27.5, 27.16, 
25.53, 26.56, 24.66, 24.55, 24.68, 25.14, 24.87, 27.14, 25.27, 
24.91, 25.32, 24.54, 25.63, 24.81, 25.47, 25.39, 25.32, 24.78, 
26.57, 25.95, 26.87, 27.17, 26.22, 26.29, 25.25, 28.22, 24.85, 
25.36, 26.98, 25.21, 25.77, 24.91, 25.17, 25.13, 25.17, 25.24, 
24.87, 24.68, 24.7, 25.01, 24.79, 25.43, 25.7, 26.15, 26.18, 
25.06, 26.63, 25.94, 27.58, 26.78, 27, 29.61, 26.22, 24.58, 26.57, 
26.14, 25.96, 25.4, 25.51, 25.31, 25.47, 25.37, 26.82, 24.84, 
24.58, 25.61, 25.42, 27.35, 26.95, 24.88, 25.4, 24.9, 25.36, 
25, 28.6, 24.89, 25.39, 25.55, 25.83, 24.78, 27.19, 27.66, 24.8, 
24.81, 25.23, 28, 25.89, 25.02, 24.73, 26.39, 26.47, 25.97, 24.64, 
25.08, 25.36, 26.7, 25.05, 26.7, 25.78, 25.08, 25.24, 26.63, 
26.26, 25.1, 27.41, 27.42, 25.92, 24.58, 24.84, 26.53, 25.68, 
26.33, 27.22, 25.31, 25.66, 26.32, 24.57, 26.13, 25.13, 24.74, 
25.59, 24.57, 25.15, 24.64, 24.92, 25.28, 25.33, 24.99, 25.15, 
24.88, 27.58, 25.17, 25.31, 25.71, 24.82, 25.87, 25.11, 25.49, 
25.45, 25.28, 27.09, 27, 25.75, 25.31, 24.67, 25.28, 25.75, 26.91, 
24.93, 25.41, 25.7, 26.15, 26.27, 25.49, 25.35, 25.41, 25.83, 
25.38, 25.94, 26.44, 25.01, 25.62, 25.76, 26.16, 25.65, 26.66, 
24.76, 24.75, 28.5, 26.76, 26.52, 25.61, 25.51, 26.5, 28.09, 
26.37, 25.13, 25.59, 25.38, 24.72, 26.47, 26.49, 25.49, 25.4, 
27.92, 24.84, 27.99, 26.8, 24.58, 25.22, 24.75, 25.52, 26.25, 
26.3, 27.29, 24.95, 27.54, 26, 26.45, 26.88, 24.51, 25.81, 27.48, 
25.22, 25.29, 25.95, 25.21, 26.38, 25.22, 25.99, 25.67, 27, 24.97, 
24.74, 26.3, 26.55, 25.02, 25.24, 25.1, 25.17, 24.95, 25, 26.52, 
24.7, 26.31, 25.68, 25.27, 25.19, 25.07, 27.34, 26.08, 27.12, 
25.53, 26.33, 25.13, 25.21, 25.53, 25.19, 27.8, 27.62, 25.57, 
26.18, 25.25, 25.1, 24.56, 25.61, 25.61, 26.18, 25.63, 26.69, 
25.56, 26.21, 25.42, 26.45, 26.03, 25.99, 25.23, 26.22, 25.18, 
25.17, 25.27, 24.83, 26.96, 25.43, 25.09, 25.52, 25.74, 25.52, 
25.22, 25.1, 25.53, 25.35, 25.18, 26.75, 26.52, 24.8, 26.3, 26.4, 
25.01, 28.4, 25.34, 25.14, 26.99, 24.65, 24.77, 25.18, 25.89, 
26.29, 26.01, 26.05, 26.77, 25.5, 24.62, 25.21, 26.21, 25.53, 
26.54, 25.44, 24.84, 24.76, 25.58, 27.2, 25.07, 25.23, 25.68, 
25.17, 25.7, 27.02, 25.46, 25.05, 24.92, 25.15, 24.66, 25.41, 
26.48, 25.16, 24.57, 27.27, 26.74, 24.88, 25.89, 25.91, 25.26, 
26.62, 24.94, 26.4, 24.79, 25.56, 24.97, 24.2, 24.18, 25.15, 
26.35, 27.25, 25.22, 25.8, 24.55, 25.95, 24.39, 26.42, 24.27, 
25.11, 25.09, 26.13, 24.84, 24.49, 24.7, 25.14, 25.05, 25.54, 
27.19, 26.79, 24.95, 26.6, 25.58, 26.28, 25.25, 26.41, 25.92, 
26.67, 25.03, 24.25, 25.9, 25.38, 25.52, 25.27, 25.93, 26.2, 
24.49, 24.21, 25.11, 25.76, 25.47, 26.09, 24.23, 24.36, 25.81, 
25.45, 25.38, 25.38, 24.4, 26.68, 24.17, 24.75, 24.69, 24.4, 
24.66, 25.68, 24.96, 25.88, 24.89, 25.49, 25.23, 25.43, 24.57, 
24.37, 24.41, 24.38, 24.91, 26.21, 25.31, 26.22, 24.47, 25.4, 
25.4, 26.01, 25.96, 25.4, 25.04, 24.79, 26.14, 26.43, 24.76, 
24.63, 25.14, 24.62, 24.83, 26.38, 26.75, 25.43, 24.68, 25.14, 
24.78, 25.85, 25.04, 26.19, 27.04, 24.58, 24.52, 26.25, 26.43, 
24.69, 26.68, 24.78, 24.95, 24.59, 24.47, 24.65, 25.14, 25.46, 
26.3, 24.98, 26.42, 25.14, 26.28, 26.7, 26.2, 26.46, 25.63)), row.names = c(NA, 
1890L), class = "data.frame")

Upvotes: 1

Views: 1483

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269774

I just noticed that someone else already posted a quantile regression solution but I initially wrote the following before I saw that so just in case this is useful I am posting this too. This shows the indicated quantiles superimposed on the points.

library(tidyr)
library(quantreg)
library(ggplot2)

tau <- c(0.01, 0.25, 0.50, 0.75, 0.99)
fm <- rq(y ~ x, data = df, tau = tau)
fit <- fitted(fm)
colnames(fit) <- tau
fitx <- data.frame(x = df$x, fit, check.names = FALSE)
long <- pivot_longer(fitx, -1, names_to = "tau")
long$tau <- factor(long$tau, levels = rev(tau))  # reverse for legend

ggplot(df, aes(x, y)) + 
  geom_point() +
  geom_line(aes(x, value, col = tau), long, lwd = 2)

(continued after plot) screenshot

Normally one needs the fitted model so you might as well use it but if not then this can be used::

  ggplot(df, aes(x, y)) + 
    geom_point() +
    geom_quantile(quantiles = tau)

Upvotes: 2

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

reg <- df %>% 
  group_by(x) %>% 
  summarise(maximum = max(y),
            minimum = min(y))

mod.max = lm(maximum ~ x, reg)
mod.min = lm(minimum ~ x, reg)

ggplot(data = df, aes(x = x, y = y)) + geom_point() +
  geom_line(data=reg, aes(y=fitted(mod.max), x=x), size=2, color="red") +
  geom_line(data=reg, aes(y=fitted(mod.min), x=x), size=2, color="green")

Output:

enter image description here

To add the R² you can use geom_label. You could do the same foe the formulas, but i think it's best on the legend:

r.max = paste("R²=", round(summary(mod.max)$r.squared, 2), sep="")
r.min = paste("R²=", round(summary(mod.min)$r.squared, 2), sep="")

ggplot(data = df, aes(x=x, y=y)) + geom_point() +
  geom_line(data=reg, aes(y=fitted(mod.max), color="Maximum ~ x", x=x), size=2) +
  geom_line(data=reg, aes(y=fitted(mod.min), color="Minimum ~ x", x=x), size=2) +
  scale_color_manual(values=c("darkred","darkgreen"), name="Regressions") +
  geom_label(aes(x=0.27, y=28, label=r.max), fill="White")
  geom_label(aes(x=0.30, y=24, label=r.min), fill="White")

enter image description here

Another option for the legend:

eq.max = paste("Maximum = ", round(mod.max$coefficients[1],1), " - ", abs(round(mod.max$coefficients[2],1)), "x", sep="")
eq.min = paste("Minimum = ", round(mod.min$coefficients[1],1), " + ", abs(round(mod.min$coefficients[2],1)), "x", sep="")

ggplot(data = df, aes(x=x, y=y)) + geom_point() +
  geom_line(data=reg, aes(y=fitted(mod.max), color=eq.max, x=x), size=2) +
  geom_line(data=reg, aes(y=fitted(mod.min), color=eq.min, x=x), size=2) +
  scale_color_manual(values=c("darkred","darkgreen"), name="Regressions") +
  geom_label(aes(x=0.27, y=28, label=r.max), fill="White") +
  geom_label(aes(x=0.30, y=24, label=r.min), fill="White")

enter image description here

Upvotes: 1

Roland
Roland

Reputation: 132736

From a scientific or statistics point of view it would seem much more useful (and common) to model quantiles instead.

E.g., for 95 % coverage interval:

library(quantreg)

fit <- rq(y ~ x, tau = c(0.025, 0.975), data = df)
summary(fit)
#Call: rq(formula = y ~ x, tau = c(0.025, 0.975), data = df)
#
#tau: [1] 0.025
#
#Coefficients:
#             Value     Std. Error t value   Pr(>|t|) 
#(Intercept)  24.66750   0.07167  344.16427   0.00000
#x            -1.25000   0.44790   -2.79080   0.00531
#
#Call: rq(formula = y ~ x, tau = c(0.025, 0.975), data = df)
#
#tau: [1] 0.975
#
#Coefficients:
#             Value     Std. Error t value   Pr(>|t|) 
#(Intercept)  30.27625   0.23898  126.69150   0.00000
#x           -11.37500   1.44528   -7.87045   0.00000

newdata <- data.frame(x = seq(from = min(df$x), 
                             to = max(df$x), 
                             length.out = 1e3))
newdata <- cbind(newdata, predict(fit, newdata = newdata))

names(newdata)
library(ggplot2)
    ggplot(data = df, aes(x = x, y = y)) + geom_point() +
  geom_line(data = newdata, aes(y = `tau= 0.025`, color = "2.5 %"), size = 2) +
  geom_line(data = newdata, aes(y = `tau= 0.975`, color = "97.5 %"), size = 2) +
  annotate("label", x = 0.32, y = 30, 
           label = paste(capture.output(coef(fit)), collapse = "\n"), hjust = 1)

resulting plot

Upvotes: 3

Related Questions