运行下面的代码:
library(ggplot2)
library(dplyr)
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6) +
theme(
text = element_blank(), # Remove text elements
line = element_blank(), # Remove line elements
rect = element_blank() # Remove rectangle elements
)
返回以下错误:
Error in `as.unit()`:
! object is not coercible to a unit
使用
theme_void()
代替消除了错误,但我不明白是什么导致了错误。谁能帮忙解释一下吗?
编辑:
我尝试单独删除每个主题对象(例如,删除
line
、运行 text
和 rect
等)并重新运行相同的代码。
这个问题似乎与
text = element_blank()
有关,因为下面的代码运行没有错误。
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6) +
theme(
line = element_blank(),
rect = element_blank(),
)
添加
text = element_blank()
由于某种原因会导致错误。
EDIT2:我正在运行 R 版本 4.3.2 (2023-10-31) 和 ggplot2_3.5.0。
我在执行“ ax$grobs[[1]]$x 时遇到同样的问题 <-ax$grobs[[1]]$x - grid::unit(1, "npc") + grid::unit(0.15, "cm") "