I have a scatter plot and I have to show boundaries of y values on it.
geom_smooth
indicates mean or median values, but I have to indicate maximum and minimum values.
How to plot the green lines?
Code:
library(ggplot2)
suaon <- mtcars
pla <- ggplot(data = suaon, mapping = aes(x = mpg, y = wt))
pla <- pla geom_point(mapping = aes(color = cyl), size = 1.4)
pla <- pla geom_smooth(span = 0.4, method = "loess", se = FALSE,color = "red", size = 0.8, alpha = 0.1)
pla <- pla labs(x = "MPG", y = "WT")
pla
CodePudding user response:
If your looking for a simple solution then you can use two for
loops. One for the maximum values and one for the minimum values.
I haven't tidied up the code and I'm sure there are better ways but this works.
library(tidyverse)
library(ggplot2)
suaon <- mtcars
##arrange by wt so that the largest wt values are first in the data frame.
max_dat <- suaon %>% arrange(-wt) %>% mutate(max_val = F)
##loop through dataframe checking if the mpg value for i is greater than the current mpg value
##if it is, then include this point as a maximum value and update current_x to the mpg value for row i.
current_x = 0
for(i in 1:nrow(max_dat)){
if(max_dat$mpg[i] > current_x){
max_dat$max_val[i] <- T
current_x <- max_dat$mpg[i]
}
}
max_dat <- max_dat %>% filter(max_val == T)
min_dat <- suaon %>% arrange(mpg) %>% mutate(min_val = F)
current_min = max(min_dat$wt)
i <- 1
for(i in 1:nrow(dat)){
if(min_dat$wt[i] < current_min){
min_dat$min_val[i] <- T
current_min <- min_dat$wt[i]
}
}
min_dat <- min_dat %>% filter(min_val == T)
pla <- ggplot(data = suaon, mapping = aes(x = mpg, y = wt))
pla <- pla geom_point(mapping = aes(color = cyl), size = 1.4)
pla <- pla geom_smooth(span = 0.4, method = "loess", se = FALSE,color = "red", size = 0.8, alpha = 0.1)
pla <- pla labs(x = "MPG", y = "WT")
pla <- pla geom_line(data = max_dat, aes(x = mpg, y = wt))
pla <- pla geom_line(data = min_dat, aes(x = mpg, y = wt))