所以這是一個非常簡短的代碼示例:
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.
這是一個非常煩人的錯誤,我還在論壇中嘗試了其他解決方案,但這似乎確實是 R 人應該注意的事情。通過添加一個簡單的 theme_bw() (或類似的)引數來防止這個錯誤,但它會取消 theme() 中的 text 引數
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()
這有效,但不應用axis.text。
有什么建議么?
我除了在軸上得到一個帶有粗體文本的圖形,它從未發生過
uj5u.com熱心網友回復:
這里的問題panel.grid是 由線條組成,因此要修改的主題元素是element_line()。
如果要更改填充,則應使用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"))
結果:

另一方面,如果您嘗試更改網格線顏色,那么您應該使用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"))
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527534.html
標籤:rggplot2主题
