R中的calc()函数错误

calc() function Error in R

我正在使用 (http://ggplot2.tidyverse.org/reference/geom_density_2d.html#computed-variables) 中的示例生成密度图。但是,我有以下错误:

library(ggplot2)
library(raster)
dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsmall, aes(x, y))
d + stat_density_2d(geom = "raster", aes(fill = calc(density)), contour = FALSE)

# Error in (function (classes, fdef, mtable)  : 
# unable to find an inherited method for function ‘calc’ for signature    
# ‘"standardGeneric", "missing"’

我错过了什么吗? density 是一些预先计算的对象吗?

有人可以帮忙吗?

你能试试这个吗?

d + stat_density_2d(geom = "raster", aes(fill = ..density..), contour = FALSE)

并查看此处的文档:https://www.rdocumentation.org/packages/ggplot2/versions/2.2.1/topics/geom_density_2d