我在R中使用geom_density_2d遇到错误

时间:2018-10-04 04:43:42

标签: r ggplot2

我是R的新手,也是编程的新手。我目前正在尝试在ggplot中使用geom_density_2d使用忠实的数据集绘制一个简单的热图。这是我要运行的代码:

library(tidyverse)

m <- faithful %>%  ggplot(aes(x = eruptions, y = waiting)) +
geom_point() +
xlim(0.5, 6) +
ylim(40, 110)
m + geom_density_2d() 

m + geom_density_2d(aes(fill = stat(level)), geom = "polygon") 

第一个图形工作正常,但是当我尝试运行第二个图形时,出现以下错误消息:

“状态(级别)错误:找不到函数“状态””

关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

使用统计信息https://ggplot2.tidyverse.org/reference/stat.html的示例,我能够重现相同的错误

library(ggplot2)
ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = stat(count)))

但是,由于我的某些软件包已经过时,因此我可以通过以下方法解决该问题:1)重新启动R和2)运行:

install.packages("tidyverse", dependencies = TRUE)