Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding scale bar to ggplot map

Tags:

I've produced this map in ggplot2:

library(maptools); library(ggplot2) data(wrld_simpl) world <- fortify(wrld_simpl) worldUk <- subset(world, id=="GBR") ggplot() + geom_path(aes(long, lat, group=group), data=worldUk, color="black", fill=NA) +   coord_equal() 

enter image description here

Using photoshop, I've added a scale bar to the map. How can I add a similar scale bar using ggplot2? This post adds a scale bar, but the bar doesn't refer to distance: scale bar and north arrow on map-ggplot2

like image 789
luciano Avatar asked Jun 17 '13 15:06

luciano


2 Answers

There is a package called ggsn, which allows you to customize the scale bar and north arrow.

ggplot() +   geom_path(aes(long, lat, group=group), data=worldUk, color="black", fill=NA) +   coord_equal() +   ggsn::scalebar(worldUk, dist = 100, st.size=3, height=0.01, dd2km = TRUE, model = 'WGS84') 

enter image description here

like image 110
rafa.pereira Avatar answered Sep 21 '22 12:09

rafa.pereira


Another option is annotation_scale from the ggspatial package. Some more hints on this blog post.

library(ggplot2) library(maptools) data(wrld_simpl)  world_sf <- sf::st_as_sf(wrld_simpl) worldUk <- subset(world_sf, NAME == 'United Kingdom') 

Plot the layer and add the scale bar.

library(ggspatial)  ggplot() +   geom_sf(data = worldUk) +   annotation_scale() 

enter image description here

like image 28
nniloc Avatar answered Sep 19 '22 12:09

nniloc