Having Trouble Making ggplot 3D Using plot_gg

447 Views Asked by At

I made a ggplot of COVID cases in March 2020 across US states, but when I try and run the plot_gg function, it shows as a flat image. I feel like I am not telling the function what the height variable should be (count in this case), but with anything I try, it either does not work or messes up the map entirely.

# Libraries
library(leaflet)
library(tidyverse)
library(ggmap)
library(leaflet.extras)
library(htmltools)
library(ggplot2)
library(maps)
library(mapproj)
library(mapdata)
library(rayshader)
library(viridis)

# US States
s <- map_data('state')
ggplot(s, aes(x=long, y=lat, group = group, fill = region)) +
  geom_polygon(color='black') +
  coord_map('polyconic') +
  guides(fill = F)

#COVID DAta -USA
c <- read.csv(file.choose(), header = T)
usa <- c %>% filter(country  == 'United States')
usa <- usa %>% group_by(province)%>%
  summarise(count=n())%>%
  arrange(desc(count))

#Merge Data
usa$province<- tolower(usa$province)
data <- merge(s, usa,
              by.x = 'region',
              by.y = 'province')

#ggplot 
 gg_usa = ggplot(data, aes(x=long, y=lat,
      group=group,
      fill=count)) +
  geom_polygon(color='grey') +
  coord_map('polyconic') +
  scale_fill_gradient2(low='white', high='purple') +
  theme_void() +
  ggtitle('COVID CASES MARCH 2020')


#Plot3d
plot_gg(gg_usa, multicore = TRUE, width = 6, height = 4)

enter image description here

1

There are 1 best solutions below

0
On

I think you might be missing the sf package. Then you can use geom_sf not geom polygon, e.g.

geom_sf(aes(fill = Count)) +

Check the North Carolina example at https://www.tylermw.com/3d-ggplots-with-rayshader/

If you use geom polygon then I think you need to add a scale argument to plot_gg