2

I'm trying to add a 20km scale bar to my map but every solution I've tried either adds the scale bar off screen (you can only see the bottom of the "km") or doesn't add it at all.

The closest I've come has been using scalebar(), which adds a scale bar off screen but doesn't allow me to move it to be fully visible. I've also tried making a bar from scratch with geom_line etc but that did not plot at all.

Here is a reproducible map without an attempt to make a scale bar and a small set of coordinates

library(ggmap)
library(ggsn)

wd <- getwd()

Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)

Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)

graphingdata <- cbind.data.frame(Latitude,Longitude)

# compute the bounding box
bc_bbox <- make_bbox(lat = as.numeric(graphingdata$Latitude), lon = as.numeric(graphingdata$Longitude))

bc_bbox

# grab the map from google


site_map <- get_stamenmap(bc_bbox, zoom = 10, maptype = "terrain")

#create and save the map
png(file=paste0(wd,"stack-ex-graph.png"))

map <- ggmap(site_map, legend = "bottom") + 
  geom_point(data = graphingdata, aes(x = as.numeric(Longitude), 
                                      y = as.numeric(Latitude)), color = "red", size = 2) + 

    ggtitle(paste0("This is the map title"), 
          subtitle = paste0("This is the subtitle"))
print(map)
dev.off()
dr39482
  • 21
  • 1
  • Maybe you can look at [this](https://stackoverflow.com/questions/18136468/is-there-a-way-to-add-a-scale-bar-for-linear-distances-to-ggmap) – ophdlv Aug 30 '19 at 13:46

1 Answers1

0

I ended up being able to use the anchor argument to shift the location of the scale bar. Because of the scope of the project I made the anchor and bounding of the map flexible.

#Create the data frame    
Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)

    Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)

    graphingdata <- cbind.data.frame(Latitude,Longitude)

#Set up bounding box
    height <- max(graphingdata$Latitude) - min(graphingdata$Latitude)
    width <- max(graphingdata$Longitude) - min(graphingdata$Longitude)
    sac_borders <- c(bottom  = min(graphingdata$Latitude)  - 0.1 * height, 
                     top     = max(graphingdata$Latitude)  + 0.1 * height,
                     left    = min(graphingdata$Longitude) - 0.1 * width,
                     right   = max(graphingdata$Longitude) + 0.1 * width)

 #Get the site map
 map <- get_stamenmap(sac_borders, zoom = 10, maptype = "terrain")    
 map <- ggmap(site_map, legend = "bottom")+ 
          scalebar(x.min = sac_borders[[3]]-1, x.max = sac_borders[[4]]+1,y.min = sac_borders[[1]]-1, y.max = sac_borders[[2]]+1,transform = TRUE,
                   dist = 20,dist_unit = "km",model = "WGS84",anchor = c(x=sac_borders[[3]]+0.6,y=sac_borders[[1]]+0.25), st.size = 2, border.size = 0.5)+
          geom_point(data = graphingdata, aes(x = as.numeric(Longitude), 
                                              y = as.numeric(Latitude)), color = "red", size = 2) + 
          ggtitle(paste0("This is the map title"), 
                  subtitle = paste0("This is the subtitle"))

Example output map. Scale bar can be moved with the anchor argument. Zoom can be modified in get_stamenmap(). Example output map.

dr39482
  • 21
  • 1