[GIS] Wrong scale in ggplot2 map

ggplot2mappingscale

I am trying to create a political map of Morocco with regions, using ggplot2, but the scale seems to be wrong. This is the map:

enter image description here

And below is the code I used:

library(ggplot2)
library(ggspatial)

YK = map_data("world")

ggplot() + 
  theme_bw() + 
  geom_polygon(data = YK, aes(x=long, y = lat, group = group), color = 'black', 
               fill = "cornsilk") + 
  geom_polygon(data = latlong, aes(x = long, y = lat, group = group), 
               color = 'black', fill = 'red3') +
  coord_sf(xlim = c(-20, 4), ylim = c(18, 38), expand = FALSE) + 
  xlab("Longitude") + ylab("Latitude") +
  ggtitle("Kingdom of Morocco", subtitle = "(Administrative Division - Regions)") +
  theme(panel.grid.major = 
          element_line(color = gray(0.5), linetype = "dashed", size = 0.5),
        panel.background = element_rect(fill = "aliceblue")
       ) + 
  annotation_north_arrow(location = "tl", style = north_arrow_fancy_orienteering)+ 
  annotation_scale(location = "br", plot_unit = "km")

Best Answer

I suspect your scale is wrong because annotation_scale() automatically uses map units based on your projection to determine the proportion of the scale bar. I recommend trying out the scalebar() function, which works well with ggplot. So in your case, instead of:

+ annotation_scale(location = "br", plot_unit = "km") at the end of your code, try adding:

+ scalebar(data = YK, location = "bottomright", dist = 4,
  dist_unit = "km", transform = TRUE,  model = "WGS84")

This assigns a distance of 4 km to each segment of the scale bar. You can change the 'model' option based on the specific ellipsoid model your map uses. Transform should be set to true if the units of the model are in decimal degrees. See the R Documentation for scalebar() to add other options regarding the size, color, and position of the scale bar.