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()