Skip to content

Instantly share code, notes, and snippets.

@dlebauer
Created April 1, 2020 22:44
Show Gist options
  • Save dlebauer/c303980b3a71675d02184caf6bd01ea9 to your computer and use it in GitHub Desktop.
Save dlebauer/c303980b3a71675d02184caf6bd01ea9 to your computer and use it in GitHub Desktop.
plot of states in southwest US using rnaturalearth and sf packages based on https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html
library(rnaturalearth)
library(rnaturalearthdata)
devtools::install_github('ropensci/rnaturalearthhires')
library(rnaturalearthhires)
library(ggspatial)
usa_states <- ne_states(country = 'United States of America', returnclass = 'sf')
state_points <- data.frame(state = usa_states$name,
st_coordinates(st_centroid(usa_states$geometry)))
ggplot(data = usa_states) +
geom_sf(fill= "antiquewhite") +
geom_text(data = state_points,
aes(x = X, y = Y, label = state), color = "darkblue", fontface = "bold", check_overlap = FALSE) +
annotate(geom = "text", x = -90, y = 26, label = "Gulf of Mexico", fontface = "italic", color = "grey22", size = 6) +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"), style = north_arrow_fancy_orienteering) +
coord_sf(xlim = c(-125, -105), ylim = c(30, 40), expand = FALSE) +
xlab("Longitude") +
ylab("Latitude") +
ggtitle("Southwest US") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), panel.background = element_rect(fill = "aliceblue"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment