R 向ggmap添加北箭头和比例尺的省钱方法

R 向ggmap添加北箭头和比例尺的省钱方法,r,ggplot2,ggmap,R,Ggplot2,Ggmap,我正在尝试使用ggmap创建一张保护区地图,我正在使用谷歌地球的卫星图像。除了缺少北箭头和比例尺外,我可以制作出非常令人满意的图像: 我知道添加这些元素有很多繁琐的方法(例如),但肯定有更省钱的方法 我试过使用map.scale和north.arrow,但这两种方法都给了我: Error in polygon(xb + arrow.x * s, yb + arrow.y * s, ...) : plot.new has not been called yet 我可以使用plot让map

我正在尝试使用ggmap创建一张保护区地图,我正在使用谷歌地球的卫星图像。除了缺少北箭头和比例尺外,我可以制作出非常令人满意的图像:

我知道添加这些元素有很多繁琐的方法(例如),但肯定有更省钱的方法

我试过使用
map.scale
north.arrow
,但这两种方法都给了我:

Error in polygon(xb + arrow.x * s, yb + arrow.y * s, ...) : 
  plot.new has not been called yet
我可以使用
plot
map.scale
north.arrow
在R底工作,但这样我就无法让我的卫星图像正确绘图。我也可以在BaseR中使用
箭头
文本
来获得我想要的东西,但是这些在ggmap中同样不起作用

下面是我正在使用的代码。您将没有多边形(因此我不会将其包含在代码中),但您将能够加载google earth图像并复制错误

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")
库(rgdal)
图书馆(ggmap)
图书馆(GISTools)
#加载卫星图片

map.center它看起来像
map.scale
north.arrow
设计用于基本图形,但ggplot使用
网格
图形。我对绘制空间数据图形不太熟悉,但作为对North arrow的快速破解,下面的代码包括两个不同的选项:

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

我倾向于使用自己的函数在地图上绘制比例尺。这使您能够精确地控制它的布局方式。比如说,

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(y-w*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) 

ggsn的
ggsn
软件包不适合您?()此外,如果你真的很在乎简洁,“简洁”是少3个字符和$BIGWORD。这是一个适合我的:我喜欢ggsn的外观,但与legendMapNice相比,它太过繁琐,无法与ggmap一起工作。我用了一个米单位的投影。调整了以下部分
text=c(如.character(seq(0.0,((n+1)*d)/1000,by=d/1000)),单位)
以在比例尺中显示公里数。