Last active
January 14, 2022 19:37
-
-
Save elipousson/df6720a940936e016b7a31a1c33c5d8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# inspirational example https://gist.github.com/ryanpeek/99c6935ae51429761f5f73cf3b027da2 | |
# libraries | |
library(ggplot2) | |
library(dplyr) | |
library(purrr) | |
library(sf) | |
library(ggplot2) | |
library(mapbaltimore) | |
library(patchwork) | |
# areas <- neighborhoods | |
areas <- council_districts | |
# Set parameters for mixed-up map | |
scale_min <- 0.3 # Scale down by scale_min percent | |
scale_max <- 0.3 # Scale up by scale_max percent | |
rotate_deg <- 40 # Rotate up to rotate_deg degrees left or right | |
# Make scale/rotate function | |
scale_rotate <- function(x, scale = 1, rotate = 0) { | |
# rotate function (see here: https://r-spatial.github.io/sf/articles/sf3.html#affine-transformations | |
rot <- function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2) | |
crs <- st_crs(x) | |
geom <- st_geometry(x) | |
center <- st_centroid(geom) | |
y <- (geom - center) * rot(pi / (360 / (rotate * 2))) | |
y <- y * scale + center | |
st_geometry(x) <- y | |
st_crs(x) <- crs | |
x | |
} | |
area_list <- areas |> | |
st_transform(4326) |> | |
nest_by(name, .keep = TRUE) | |
city_layer <- | |
list( | |
geom_sf(data = baltimore_city_detailed, fill = NA, color = "gray40", size = 0.3), | |
pilot::scale_fill_pilot(), | |
theme_void() | |
) | |
area_map <- | |
ggplot() + | |
geom_sf(data = areas, aes(fill = name), alpha = 0.4, show.legend = FALSE, size = 0.2) + | |
city_layer | |
area_mixup_map <- | |
ggplot() + | |
map( | |
area_list$data, | |
~ geom_sf( | |
data = scale_rotate( | |
.x, | |
runif(1, min = 1 - scale_min, max = 1 + scale_max), | |
runif(1, min = -1 * rotate_deg, max = rotate_deg) | |
), | |
aes(fill = name), alpha = 0.4, show.legend = FALSE, size = 0.2 | |
) | |
) + | |
city_layer | |
area_map + area_mixup_map + | |
plot_layout( | |
ncol = 2, | |
nrow = 1, | |
widths = c(0.5, 0.5) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment