tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.06k stars 214 forks source link

plot_gg using geom_density2d_filled() - Error: Discrete variable cannot be mapped to 3D #148

Closed storopoli closed 3 years ago

storopoli commented 3 years ago

Guys, I'm trying to use ray shader to generate a 3-D visualization of a bivariate normal distribution using MASS::mvrnorm and I get a strange error:

library(ggplot2)
library(mnormt)
library(MASS)
library(rayshader)
set.seed(123)
mus  <- c(0, 0)
sigmas <- c(1, 1)
r <- 0.8
Sigma <- diag(sigmas)
Sigma[1, 2] <- r
Sigma[2, 1] <- r
dft <- data.frame(mvrnorm(1e5, mus, Sigma))
gg <- ggplot(dft, aes(X1, X2)) +
    geom_density2d_filled()

plot_gg(gg, multicore = TRUE, width = 5, height = 5, scale = 250)
#> Error: Error: Discrete variable cannot be mapped to 3D. Did you mean to choose `color` as the `height_aes`?

Created on 2021-02-11 by the reprex package (v1.0.0)

will-rogers commented 3 years ago

I am pretty sure the geom_ filled layers return a discrete variable for coloring, which plotgg has issues with. Try using the stat histogram/density flavor of ggplot layers. Given the MVN dist, you might also consider stat_bin2d() or stat_binhex() Y

You can force these layers to return continuous values - per the opening vignette, try this:

library(ggplot2) library(mnormt) library(MASS) library(rayshader) set.seed(123) mus <- c(0, 0) sigmas <- c(1, 1) r <- 0.8 Sigma <- diag(sigmas) Sigma[1, 2] <- r Sigma[2, 1] <- r dft <- data.frame(mvrnorm(1e5, mus, Sigma)) gg <- ggplot(dft, aes(X1, X2)) + stat_density_2d(aes(fill = stat(nlevel)), geom = "polygon", n = 100, bins = 10, contour = TRUE) plot_gg(gg, triangulate = TRUE, width = 3, height = 3, scale = 250)

image

require(hexbin) gg <- ggplot(dft, aes(X1, X2)) + stat_binhex()

plot_gg(gg, raytrace = F, triangulate = TRUE, width = 3, height = 3, scale = 250)

image

storopoli commented 3 years ago

Thank you very much!

tylermorganwall commented 3 years ago

Thanks for handling this one, @wilrogers!