I have a data frame of x and y values.
I have a custom line defined with a slope and intercept. Not a regression.
How can I subset values from the data frame that are above the line?
I would like the make a new column in the data frame with a categorical variable that denotes "above line" and "below line".
Reproducible example:
set.seed(12)
x<-runif(100,min=1,max=700)
y<-runif(100,min=1,max=350)
df<-data.frame(x,y)
ggplot(df, aes(x=x,y=y))
geom_point()
geom_abline(aes(intercept=187.835,slope=-0.309), color="red")
CodePudding user response:
Create an ifelse
statement:
df <- df %>%
mutate(new = ifelse(y > 187.185 - 0.309*x, "above", "below"))
df %>%
ggplot(aes(x=x,y=y,color = new))
geom_point()
geom_abline(aes(intercept=187.835,slope=-0.309), color="red")
You can also filter using filter
:
df %>%
filter(new == "above")