Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Created November 12, 2023 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliocamp/07ed7c493ee4be8cef60f3592c742d7c to your computer and use it in GitHub Desktop.
Save eliocamp/07ed7c493ee4be8cef60f3592c742d7c to your computer and use it in GitHub Desktop.
ggblend topography
library(metR)
library(data.table)
library(ggplot2)
proj <- "+proj=lcc +lon_0=-60 +lat_1=-0 +lat_2=-40"
topo <- GetTopography(ConvertLongitude(-85),
330, lat.north = 14, lat.south = -58,
resolution = 2/60) |>
_[, c("dlon", "dlat") := Derivate(h ~ lon + lat)] |>
_[, c("dx", "dy") := list(dx(dlon, lat),
dy(dlat))]
r <- 1/1.3
height <- 1600
png(filename = "~/Downloads/with_blend.png", width = height*r, height = height)
with_blend <- topo |>
ggplot(aes(lon, lat)) +
list(
geom_raster(aes(fill = sqrt(dx^2 + dy^2)), alpha = 1),
scale_fill_gradient(low = "white", high = "black", guide = "none"),
ggnewscale::new_scale_fill(),
geom_raster(data = \(x) subset(x, h> 0), aes(fill = h),alpha = 1),
scale_fill_gradientn(colors = terrain.colors(5), guide = "none"),
ggnewscale::new_scale_fill(),
geom_raster(data = \(x) subset(x, h < 0), aes(fill = h), alpha = 1),
scale_fill_gradientn(colors = topo.colors(10)[1:4], guide = "none"),
ggnewscale::new_scale_fill()
) |> ggblend::blend("multiply") +
geom_contour2(aes(z = h), breaks = 0) +
coord_sf(crs = proj) +
theme_void()
print(last_plot())
dev.off()
png(filename = "~/Downloads/no_blend.png", width = height*r, height = height)
no_blend <- topo |>
ggplot(aes(lon, lat)) +
list(
geom_raster(data = \(x) subset(x, h> 0), aes(fill = h),alpha = 1),
scale_fill_gradientn(colors = terrain.colors(5), guide = "none"),
ggnewscale::new_scale_fill(),
geom_raster(data = \(x) subset(x, h < 0), aes(fill = h), alpha = 1),
scale_fill_gradientn(colors = topo.colors(10)[1:4], guide = "none"),
ggnewscale::new_scale_fill()
) |> ggblend::blend("multiply") +
geom_contour2(aes(z = h), breaks = 0) +
coord_sf(crs = proj) +
theme_void()
print(last_plot())
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment