将北方箭头和比例尺添加到ggmap中的方法很简单 [英] Parsimonious way to add north arrow and scale bar to ggmap

查看:576
本文介绍了将北方箭头和比例尺添加到ggmap中的方法很简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用ggmap来创建我正在使用来自Google地球下方的卫星图像的保护区域的地图。我可以制作一张非常令人满意的图片,只是它没有北向箭头和比例尺:



我知道有很长时间可以添加这些元素(例如


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?

解决方案

It looks like map.scale and north.arrow are designed to work with base graphics, but ggplot uses grid graphics. I'm not that familiar with graphing 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"))  

这篇关于将北方箭头和比例尺添加到ggmap中的方法很简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆