business-science / free_r_tips

Free R-Tips is a FREE Newsletter provided by Business Science. It comes with bite-sized code tutorials every week.
1.38k stars 699 forks source link

018 - Rayshader #10

Open NCdJ opened 1 year ago

NCdJ commented 1 year ago

There's an error when i run the script...

> # ggplot (2D visualization) - DS4B 101-R, Week 4
> g1 <- mtcars %>%
+     ggplot(aes(disp, mpg, color = cyl)) +
+     geom_point(size=2) +
+     scale_color_continuous(limits=c(0,8)) +
+     ggtitle("mtcars: Displacement vs mpg vs # of cylinders") +
+     theme(title = element_text(size=8),
+           text = element_text(size=12))
> # rayshader
> g1 %>%
+     plot_gg(
+         height        = 3,
+         width         = 3.5,
+         multicore     = TRUE,
+         pointcontract = 0.7,
+         soliddepth    = -200
+     )
Error in hillshade[, , 1] * scales::rescale(shadowmap, c(max_darken, 1)) : 
  non-conformable arrays
> g1 %>%
+     plot_gg(
+         height        = 3,
+         width         = 3.5,
+         multicore     = TRUE,
+         pointcontract = 0.7,
+         soliddepth    = -200
+     )
Error in hillshade[, , 1] * scales::rescale(shadowmap, c(max_darken, 1)) : 
  non-conformable arrays
> # 3D ELEVATION MATRIX ----
> volcano
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]  100  100  101  101  101  101  101  100
 [2,]  101  101  102  102  102  102  102  101
 [...]

 > volcano %>% class()
[1] "matrix" "array" 
> # dplyr (data wrangling) - DS4B 101-R, Weeks 2&3
> volcano_tbl <- volcano %>%
+     as_tibble(.name_repair = "minimal") %>%
+     set_names(str_c("V", seq_along(names(.)))) %>%
+     rowid_to_column(var = "x") %>%
+     pivot_longer(
+         cols      = contains("V"),
+         names_to  = "y",
+         values_to = "value"
+     ) %>%
+     mutate(y = str_remove(y, "^V") %>% as.numeric())
> # ggplot (visualization) - DS4B 101-R, Week 4
> g2 <- volcano_tbl %>%
+     ggplot(aes(x = x, y = y, fill = value)) +
+     geom_tile() +
+     geom_contour(aes(z = value), color = "black") +
+     scale_x_continuous("X", expand = c(0,0)) +
+     scale_y_continuous("Y",expand = c(0,0)) +
+     scale_fill_gradientn("Z", colours = terrain.colors(10)) +
+     coord_fixed()
> g2 %>%
+     plot_gg(
+         multicore = TRUE,
+         raytrace = TRUE,
+         width = 7,
+         height = 4,
+         scale = 300,
+         windowsize = c(1400, 866),
+         zoom = 0.6,
+         phi = 30,
+         theta = 30
+     )
Error in hillshade[, , 1] * scales::rescale(shadowmap, c(max_darken, 1)) : 
  non-conformable arrays
In addition: Warning messages:
1: The following aesthetics were dropped during
statistical transformation: fill
ℹ This can happen when ggplot fails to infer the
  correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic
  or to convert a numerical variable into a
  factor? 
2: The following aesthetics were dropped during
statistical transformation: fill
ℹ This can happen when ggplot fails to infer the
  correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic
  or to convert a numerical variable into a
  factor? 
3: Removed 1861 rows containing missing values
(`geom_contour()`). 

I don't know where to start to look for. Thanks for the help