Powerful way to add north arrow and scale bar in ggmap

I am trying to use ggmap to create a map of the protected areas in which I work, with a satellite image from Google Earth below. I can make a very satisfactory image, except that he lacks a north arrow and a scale:

enter image description here

I know that there are very long sophisticated ways to add these elements (for example, here ), but, of course, there should be a more mean way to do this!

I tried using map.scale and north.arrow , but they 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 the R database using plot , but then I can not correctly display the image of my satellite. I can also get what I want using arrows and text in the R base, but again they will not work in ggmap.

The code I'm using is below. You won’t have a polygon (so I won’t include it in the code), but you can upload a 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 a small number of readings, it seems that the map.scale and north.arrow do not recognize the window that the ggmap function creates as an open graphic window. I did some research and tried to fix it, but nothing worked. Can anyone suggest a way to fix the error I get, or get a scale scale and north arrow in ggmap without using hundreds of lines of code?

+7
r ggplot2 ggmap
source share
2 answers

It looks like map.scale and north.arrow are designed to work with basic graphics, but ggplot uses grid graphics. I am not so good at graphic spatial data, but as a quick hack for the North arrow, the code below includes two different options:

 ggmap(map, extent= "device") + geom_segment(arrow=arrow(length=unit(3,"mm")), aes(x=33.5,xend=33.5,y=-2.9,yend=-2.6), colour="yellow") + annotate(x=33.5, y=-3, label="N", colour="yellow", geom="text", size=4) + geom_segment(arrow=arrow(length=unit(4,"mm"), type="closed", angle=40), aes(x=33.7,xend=33.7,y=-2.7,yend=-2.6), colour=hcl(240,50,80)) + geom_label(aes(x=33.7, y=-2.75, label="N"), size=3, label.padding=unit(1,"mm"), label.r=unit(0.4,"lines")) 

enter image description here

+10
source share

I try to use my own function to draw scales on ggmaps. This gives you great control over how you want. For example,

 scalebar = function(x,y,w,n,d, units="km"){ # x,y = lower left coordinate of bar # w = width of bar # n = number of divisions on bar # d = distance along each division bar = data.frame( xmin = seq(0.0, n*d, by=d) + x, xmax = seq(0.0, n*d, by=d) + x + d, ymin = y, ymax = y+w, z = rep(c(1,0),n)[1:(n+1)], fill.col = rep(c("black","white"),n)[1:(n+1)]) labs = data.frame( xlab = c(seq(0.0, (n+1)*d, by=d) + x, x), ylab = c(rep(yw*1.5, n+2), y-3*w), text = c(as.character(seq(0.0, (n+1)*d, by=d)), units) ) list(bar, labs) } sb = scalebar(33.5, -3.8, 0.05, 5, 0.3, "degrees" ) # Plot map ggmap(map, extent= "device") + geom_rect(data=sb[[1]], aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=z), inherit.aes=F, show.legend = F, color = "black", fill = sb[[1]]$fill.col) + geom_text(data=sb[[2]], aes(x=xlab, y=ylab, label=text), inherit.aes=F, show.legend = F) 

enter image description here

+8
source share

All Articles