Last active
September 19, 2023 13:20
-
-
Save jmclawson/6bff76c7d671756ff56bd701fb7a25e5 to your computer and use it in GitHub Desktop.
Shift or rotate states when mapping (good for simpler US maps)
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
# This process is adapted from https://sesync-ci.github.io/blog/transform-Alaska-Hawaii.html | |
# Here, it's offered as a function to simplify trial-and-error. | |
move_state <- function( | |
df, # spatial dataframe | |
choice, # value in "state" column | |
rotation, # eg -39 * pi/180 | |
right, # amount to move eastward; use negative for left/west | |
up# amount to move northward; use negative for down/south | |
){ | |
rot <- function(a){ | |
matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2) | |
} | |
df_state <- df |> | |
filter(state == choice) | |
centroid <- df_state |> | |
st_geometry() |> | |
st_union() |> | |
st_centroid() | |
transformation <- (st_geometry(df_state) - centroid) * rot(rotation) + centroid + c(right, up) | |
df_state <- df_state |> | |
st_set_geometry(transformation) |> | |
st_set_crs(st_crs(df)) | |
df |> | |
filter(state != choice) |> | |
rbind(df_state) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mapping just the United States can result in an awkward map:
Quentin Read shows a good process to move the spatial coordinates of states, including moving, rotating, and shrinking them here: https://sesync-ci.github.io/blog/transform-Alaska-Hawaii.html
I wasn't satisfied with Alaska's shrinking because it's more inaccurate than is needed. (See Claus Wilke's Fundamentals of Data Visualization chapter 15.) Keeping Alaska its original size necessitated playing around with the numbers to get it someplace not bumping into another state. This function simplifies that trial and error. Note that it expects the data frame to have a filtering column called
state
.