rggplot2ggspatial

Annotation_scalebar showing the wrong units


When I try to plot a map in ggplot2 and add a scale bar with annotation_scalebar(), it puts the units in km. I want them in miles.

map + annotation_scale(pad_x = unit(4,"cm"), width_hint = 0.4)

Initial map, no units assigned

When I change the plot_unit argument to "mi", the scale is completely wrong and still reads in km.

map + annotation_scale(plot_unit = "mi", pad_x = unit(4,"cm"), width_hint = 0.4)

Map with plot_unit = 'mi'

I have played around with the data argument, putting in my spatial dataframe for the bbox but it did not change the output for either map.


Solution

  • The plot_unit argument is meant to set the units

    for non-coord_sf applications, specify the unit for x and y coordinates. Must be one of km, m, cm, mi, ft, or in.

    But you can switch to miles by setting unit_category = "imperial".

    Using the default example from ?ggspatial::annotation_scale:

    library(ggspatial)
    
    cities <- data.frame(
      x = c(-63.58595, 116.41214),
      y = c(44.64862, 40.19063),
      city = c("Halifax", "Beijing")
    )
    
    ggplot(cities) +
      geom_spatial_point(aes(x, y), crs = 4326) +
      annotation_scale(unit_category = "imperial") +
      coord_sf(crs = 3995)