Jake L
Jake L

Reputation: 1067

gghighlight messing up when annotations are added on facets

I have a plot that I want to facet and use gghighlight, but I also want to add some common non-data elements to the plots (a horizontal line at zero), and i'm finding that gghighlight is throwing errors when those elements are added before the faceting and highlighting codes.

reprex:

This works:

pen <- palmerpenguins::penguins
  
pen %>%
  ggplot(aes(x = bill_length_mm,
             y = bill_depth_mm,
             color = species)) + 
  geom_point() +
  facet_wrap(~species) +
  gghighlight(use_direct_label = F) +
  geom_hline(yintercept = 17.5) 

but if I want my geom_hline to be "under" the scatter plots, so move it before the geom_point line, I get an error.

pen <- palmerpenguins::penguins
  
pen %>%
  ggplot(aes(x = bill_length_mm,
             y = bill_depth_mm,
             color = species)) + 
  geom_hline(yintercept = 17.5) +
  geom_point() +
  facet_wrap(~species) +
  gghighlight(use_direct_label = F) 

which returns:

Error in `geom_hline()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error:
! object 'species' not found
Run `rlang::last_trace()` to see where the error occurred.

I've tried using inherit.aes=F in my geom_hline, but that was ignored and the error still occurred.

I just updated to R version 4.4.2.

Upvotes: 0

Views: 36

Answers (0)

Related Questions