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 fix an error with layers in geom_rect?

While creating a ggplot chart using geom_rect, I encountered the following error:

Error in `geom_rect()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'Weekly_Sales' not found

My code:

ggplot(data = df1, aes(x = Date, y = Weekly_Sales)) +
  geom_rect(data = df2, color = 'white',aes(xmin = as.Date(xmin), 
  ymin = ymin, xmax = as.Date(xmax), ymax = ymax, fill = changes),alpha = .4) +
  geom_line(color = 'orange', size = 1.25) + 
  scale_fill_manual(values = c('blue', 'green'))

The Weekly_Sales column is in the df1 dataset.

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 :

You need to add inherit.aes = F inside geom_rect.

inherit.aes
If FALSE, overrides the default aesthetics, rather than combining with them.

ggplot(data = df1, aes(x = Date, y = Weekly_Sales)) +
  geom_rect(data = df2, inherit.aes = F, color = 'white',aes(xmin = as.Date(xmin), 
  ymin = ymin, xmax = as.Date(xmax), ymax = ymax, fill = changes),alpha = .4) +
  geom_line(color = 'orange', size = 1.25) + 
  scale_fill_manual(values = c('blue', 'green'))
Add a comment

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