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 I also tried other solutions in the forum but 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?
I excepted to get a figure with bolded text on the axes, and it never happend
CodePudding user response:
The issue here is that the panel.grid
is composed of lines and so the theme element to modify is element_line()
.
If you want to change the fill, you should be using panel.background
.
mtcars %>%
ggplot(aes(x = wt, y = mpg))
geom_point()
geom_line()
theme(axis.text = element_text(face = "bold"),
panel.background = element_rect(fill = "grey"))
Result:
On the other hand if you're trying to change the grid line colour then you should be using element_line(color = ...)
:
mtcars %>%
ggplot(aes(x = wt, y = mpg))
geom_point()
geom_line()
theme(axis.text = element_text(face = "bold"),
panel.grid = element_line(color = "grey"))
Result: