I am trying to use ggmap to create a map of the protected areas I am working in with a satellite image from google earth underneath. I can make a very satisfactory image except that it lacks a north arrow and scale bar:
I'm aware that there are very long winded ways to add these elements (e.g. here) but there must surely be a more parsimonious way to do it!
I've tried using map.scale
and north.arrow
but these both give me:
Error in polygon(xb + arrow.x * s, yb + arrow.y * s, ...) :
plot.new has not been called yet
I can get both map.scale
and north.arrow
to work in base R using plot
but then I can't get my satellite image to plot properly. I can also get what I want using arrows
and text
in base R but again these won't work in ggmap.
The code I'm using is below. You won't have the polygon (so I won't include it in the code) but you'll be able to load the google earth image and replicate the error.
library(rgdal)
library(ggmap)
library(GISTools)
# Load satellite picture
map.centre <- c(lon = 35, lat = -2.5)
map <- get_map(location=map.centre, source="google", maptype="satellite", zoom = 8)
# Plot map
ggmap(map, extent= "device")
map.scale(xc= 34, yc= -3, len= 10, units= "Kilometers",
ndivs= 4, tcol= "black", scol= "black", sfcol="black")
north.arrow(xb= 35.5, yb= -1, len=100, lab="N")
From doing a bit of reading it seems like the map.scale
and north.arrow
functions aren't recognising the window the ggmap
function creates as an open graphical window. I've done some research and tried to fix this but nothing has worked. Is anyone able to suggest a way to either fix the error I'm receiving or to get a scale bar and north arrow in ggmap without using hundreds of lines of code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…