0

I am creating a map on R using ggmplot2 and ggmap, withe ggspatial package to add a north arrow and scale bar. Everything works until I try to add the scale bar with annotation_scale(location = 'tl') and the coord_sf() lines at the end. The scale bar is not to scale (see pic where it says 1cm). How do I fix this?

    # installs
    install.packages(c("ggplot2", "devtools", "dplyr", "stringr"))
    install.packages(c("maps", "mapdata", "ggmap", "sf", "ggspatial"))
    
    # imports
    library(ggplot2)
    library(ggmap)
    library(maps)
    library(mapdata)
    library(ggspatial)
    
    # register google key
    register_google("REMOVED")
    
    df <- data.frame(name=c('A', 'I', 'F', 'H', 'E', 'G', 'B', 'J', 'D', 'C', 'L', 'K'),
             lon=c(145.466733, 145.460271, 145.454133, 145.451762, 145.444134, 
                   145.466618, 145.444876, 145.441986, 145.449982, 145.449043,
                   145.445283, 145.44458),
             lat=c(-14.684977, -14.685441, -14.647456, -14.658067,  -14.6693,
                   -14.689562, -14.678148, -14.685979, -14.696519, -14.682886,
                   -14.683251, -14.679985))

    cmap = c('sienna1', 'seagreen2','sienna1', 'sienna1','deeppink2', 'sienna1','deeppink2', 'seagreen2','deeppink2', 'deeppink', 'seagreen2','seagreen2')

    # compute the mean lat and lon of coordinates
    ll_means <- sapply(df[2:3], mean)
    
    #create map
    sq_map2 <- get_map(location = ll_means,  maptype = "satellite", source = "google", zoom = 14)
    map = ggmap(sq_map2) + 
      geom_point(data = df, color = cmap, size = 4) +
      geom_text(data = df, aes(label = paste("  ", as.character(name), sep="")), hjust = 0, color = "white", size = 4) +
      labs(x = "Longitude", y = "Latitude") +
      annotation_north_arrow( height = unit(1, "cm"), width = unit(1, "cm"), pad_y = unit(0.7, "cm")) +
      annotation_scale(location = 'tl') + 
      coord_sf()
    
    map

If i don't include the coord_sf() line then it won't plot at all.

enter image description here

  • 1
    Maybe this question: https://stackoverflow.com/questions/18136468/is-there-a-way-to-add-a-scale-bar-for-linear-distances-to-ggmap/20580465#20580465 – Dave2e Sep 29 '22 at 22:07

0 Answers0