It’s taken me too long to try out rayshader’s new ggplot feature and obviously when I got around to it, I was going to find a way to use geoviz.
Here’s a quick code snippet to draw a stylised hexbin elevation map of the UK, that’s easily changed to anywhere else in the world that you’d like to try.
You can adapt this to draw contours instead, or anything else you can come up with in ggplot. Check out the Tyler Morgan-Wall’s rayshader site for more code examples.
library(rayshader)
library(geoviz)
library(tibble)
library(dplyr)
library(ggplot2)
library(raster)
# Get elevation data using geoviz
dem <- mapzen_dem(55.4, -3.44, square_km = 400, max_tiles = 20)
# Set anywhere at or below sea level to NA because we don't want the ocean to be rendered
dem_coords <- tibble::as_tibble(raster::rasterToPoints(dem)) %>%
mutate(layer = ifelse(layer <= 0, NA, layer))
# Draw a ggplot chart
plot_out <- dem_coords %>%
ggplot(aes(x = x, y = y, z = layer)) +
stat_summary_hex(bins = 50, size = 0.5, color = "black") +
scale_fill_viridis_c(option = "C")
# Render in 3D with rayshader
par(mfrow = c(1, 2))
plot_gg(plot_out, multicore = TRUE, raytrace = TRUE, width = 8, height = 8,
scale = 300, windowsize = c(1400, 866), zoom = 0.6, phi = 30, theta = 30)
# Output with depth of field
render_depth(focus = 0.8, focallength = 200, fstop = 12)