Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid "object not found" error with geom_abline?

I am trying to plot lines from a data frame with columns indicating the slope and intercept, but keep on getting an "object not found" error. Here is a reproducible example:

library(tidyverse)

df <- tibble(intercept = 1,
             slope = 0.5)

df %>% 
  ggplot() +
  geom_abline(slope = slope, intercept = intercept)
#> Error in geom_abline(slope = slope, intercept = intercept): object 'slope' not found

I recognize that I must be missing something fairly rudimentary about the operation of geom_abline, but — based on the documentation — it’s not clear to me what I’m missing. So, my questions are: (1) why does the code yield this error, and (2) how can I plot the data using ggplot?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your approach would work if you put the intercept and slope in the aesthetics, e.g.

df %>% 
  ggplot() +
  geom_abline(aes(slope = slope, intercept = intercept))

If you don’t want to use the aesthetics, you need to reference the column of the dataframe:

df %>% 
  ggplot() +
  geom_abline(slope = df$slope, intercept = df$intercept)
1 comments

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading