merge_elemnt() error with no apparent reason (ggplot2)

Hello. So here is a very short example for the code:

mtcars %>% ggplot(aes(x=wt,y=mpg,group=1))+
  geom_point()+geom_line()+
  theme(panel.grid = element_rect(fill = "grey"),
        axis.text = element_text(face = "bold"))

Error in `**merge_element()**`:
! Only elements of the same class can be merged
Run `rlang::last_error()` to see where the error occurred.

This is such an annoying error, and it really seems like something R folks should take care of. This error is prevented by adding a simple theme_bw() (or alike) argument, but it then cancels the text argument within theme()


mtcars %>% ggplot(aes(x=wt,y=mpg,group=1))+
geom_point()+geom_line()+
theme(panel.grid = element_rect(fill = "grey"),
axis.text = element_text(face = "bold"))+
theme_minimal()

This works but doesnt apply the axis.text.

Any suggestions?

panel.grid should be an element_line(). See

panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y

    grid lines (element_line()).

This does not throw and error, though it may not be what you want.

mtcars %>% ggplot(aes(x=wt,y=mpg,group=1))+
  geom_point()+geom_line()+
  theme(panel.grid = element_line(color = "grey"),
        axis.text = element_text(face = "bold"))
1 Like

Great. Thank you, I don't know why checking the cheatsheet or help file didn't occur to me. Maybe because it didn't throw an error all the time so I thought I wasn't doing anything wrong.

Regards

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.