ggplot错误-“无法将类‘c(“ ScaleContinuousPosition,” ScaleContinuous“,” Scale“,” ggproto“,”强制转换为data.frame“

时间:2019-12-06 17:24:20

标签: r ggplot2 dplyr

假设我正在对ggplot数据进行超简单的iris绘制,它看起来像这样:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_line() +
  theme_minimal() +
  scale_x_continuous(limits = c(5, 8)) %>%
  annotate(geom = "text", x = 6, y = 5, label = "We Love Flowers!", color = "#619CFF", size = 8)

由于某种原因,它会引发此错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘c("ScaleContinuousPosition", "ScaleContinuous", "Scale", "ggproto", ’ to a data.frame

有什么作用?发生了什么事?

1 个答案:

答案 0 :(得分:1)

听,我们都去过那里。在dplyrggplot之间切换,而忘记在%>%管道和+运算符之间切换。发生在我们当中最好的!

更正后的代码如下:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_line() +
  theme_minimal() +
  scale_x_continuous(limits = c(5, 8)) +
  annotate(geom = "text", x = 6, y = 5, label = "We Love Flowers!", color = "#619CFF", size = 8)