Search code examples
rggplot2density-plot

calc() function Error in R


I am using the examples at (http://ggplot2.tidyverse.org/reference/geom_density_2d.html#computed-variables) to generate a density plot. However, I have the below error:

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"’

Am I missing something? Is density some pre-computed object?

Could someone help?


Solution

  • Can you try this?

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

    And have a look to the documentation here: https://www.rdocumentation.org/packages/ggplot2/versions/2.2.1/topics/geom_density_2d