I've produced a map using the sf package and ggplot2:
library(ggplot2)
library(sf)
library(rnaturalearth)
state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"), returnclass="sf")
x <- ggplot(data=state_prov) +
geom_sf()+
coord_sf(xlim=c(-170, -95), ylim=c(40, 75))
print(x)
which produces the following map in Rstudio:
That's great, but I need to add a scale bar to it. When I try to modify the code using ggsn, I don't see the scale bar at all.
library(ggplot2)
library(sf)
library(rnaturalearth)
state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"), returnclass="sf")
x <- ggplot(data=state_prov) +
geom_sf()+
coord_sf(xlim=c(-170, -95), ylim=c(40, 75)) +
ggsn::scalebar(state_prov, location="topleft", dist = 50, dist_unit = "km",
transform=TRUE, model="WGS84", height=0.1)
print(x)
I've tried to change the height, st.dist, and location with no success. I can see a poorly scaled scalebar when I remove the call to coord_sf(), making me believe that ggsn does not recognize that the map is being zoomed in by coord_sf().
How do I fix this? ggsn does not seem to be easily modifiable. I'm open to using another package or method, but I do need to continue calling ggplot in a similar manner since I have a much more complicated map based on the same structure.
Thanks!