2

I'm trying to create an stl file based on a US map with different locations, as points, at different heights based on a data value.

I can create the points part relatively easily.

library(ggplot2)
library(data.table)
library(rayshader)

myPoints <- structure(list(N = c(7.34e+20, 1e+18, 1.471e+21, 1.35e+21, 2.096e+21
), latitude = c(35.060137, 42.151816, 34.420986, 39.713209, 32.445652
), longitude = c(-93.133718, -71.77203, -92.530547, -75.661478, 
                 -93.739031)), row.names = c(NA, -5L), 
class = c("data.table",  "data.frame"), .internal.selfref = <pointer: 0x0000000006431ef0>)


gg1 <- ggplot() +
       geom_map(data = us, map = us, aes(x = long, y = lat, map_id = region),
                fill = "#ffffff", color = "#ffffff", size = 0.15) + 
       geom_point(data = test, aes(x = longitude, y = latitude, color = N), inherit.aes = F) +
       theme(legend.position = "none")

gg1

plot_gg(gg1, multicore = TRUE, width = 5, height = 5, scale = 250, windowsize = c(1400,866),
        zoom = 0.55, phi = 30)

But, when I save to an stl using rayshader, the US portion is flat.

enter image description here

In other words, it is basically a flat surface with some spikes coming up. So I thought I would try to just add in some state level data, based off here , and that would add some elevation to the US portion.

But what happens is the elevated US portion wipes out the point data.

us <- map_data("state")

arr <- USArrests %>% 
       rownames_to_column("region") %>% 
       mutate(region = tolower(region))

arr$Murder <- 0.01


gg2 <- ggplot() +
       geom_map(data = us, map = us, aes(x = long, y = lat, map_id = region),
                fill = "#ffffff", color = "#ffffff", size = 0.15) + 
       geom_map(data = arr, map = us, aes(fill = Murder, map_id = region), color = "#ffffff") + 
       geom_point(data = test, aes(x = longitude, y = latitude, color = N), inherit.aes = F) +
       theme(legend.position = "none")

gg2

plot_gg(gg2, multicore = TRUE, width = 5, height = 5, scale = 250, windowsize = c(1400,866),
        zoom = 0.55, phi = 30)

enter image description here

Ultimatly what I would like is the US portion slightly elevated, and then my point data above that. I tried mulitply the points to make even large, etc, but I can't get it.

I am wondering if I need to forgo the rayshader ggplot function, and try to use a matrix, but I'm still learning to work with spatial data I don't know a good way to convert US map data to a matrix and correctly label the points on said matrix.

I'm open to any and all suggestions.

jazzurro
  • 23,179
  • 35
  • 66
  • 76
James Holland
  • 1,102
  • 10
  • 17

1 Answers1

0

I invested some time to learn this cool package since I did not know it till I saw this question. As far as I understood from the present CRAN manual (version 0.13.2), I think you cannot produce the graphic you are asking. In the CRAN manual (p. 15), you see that there is an argument called height_aes.

Default ‘NULL‘. Whether the ‘fill‘ or ‘color‘ aesthetic should be used for height values, which the user can specify by passing either ‘fill‘ or ‘color‘ to this argument. Automatically detected. If both ‘fill‘ and ‘color‘ aesthetics are present, then ‘fill‘ is default.

As you see, you can use EITHER fill or color to create height in graphics. By default, plot_gg() chooses fill for height. Therefore, you saw that polygons were elevating while dots did not. At the moment, I think you gotta choose either aesthetic when you produce graphics. Let's wait and see if we would be able to make both polygons and bars 3D.

jazzurro
  • 23,179
  • 35
  • 66
  • 76