2014-10-08 133 views
5

帮助!我的GPS X/Y位置在一个林木的数据集,以及与此码成一个漂亮的地图在ggplot:无法在ggplot2上绘制比例尺或北向箭头

#ggmap! 
library(ggmap) 
library(mapproj) 
map <- get_map(location = 'Madagascar', zoom = 10) 
geocode("kianjavato") 
#lon  lat 
#47.86682 -21.38024 
k <- "kianjavato" 
myMap <- get_map(location=k, source="stamen", maptype="toner", crop=FALSE, zoom=16) 
ggmap(myMap) 
m <- ggmap(myMap) + geom_point(aes(y = Lat, x = Lon, colour = Foraged, shape=Plot), data = GPS) 
n <- m + scale_colour_manual(values=c("blue", "red3")) + 
    scale_shape_discrete(solid=F, legend=F) + 
    scale_y_continuous(limits=c(-21.376,-21.3715)) + 
    scale_x_continuous(limits=c(47.865,47.869)) 
plot(n) 

然而,不管我是什么类型的代码,我不能让指向北方的箭头或比例尺在我的地图上绘图!我已经尝试了许多解决方案,其中一些看起来像这样:

尝试添加比例尺

map.scale <- ggmap(new) + (ggmap, extent = "normal", maprange = FALSE) %+% sites.data + 
    geom_point(aes(x = lon, y = lat, colour = colour)) + 
    geom_text(aes(x = lon, y = lat, label = label), hjust = 0, vjust = 0.5, size = 8/ptspermm) + 
    geom_segment(data = sbar, aes(x = lon.start, xend = lon.end, 
            y = lat.start, yend = lat.end)) + 
    geom_text(data = sbar, aes(x = (lon.start + lon.end)/2, 
           y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat), 
       label = paste(format(distance, digits = 4, nsmall = 2),'km')), 
       hjust = 0.5, vjust = 0, size = 8/ptspermm) + 
    coord_map(projection="mercator", xlim=c(bb$ll.lon, bb$ur.lon), 
       ylim=c(bb$ll.lat, bb$ur.lat)) 

#library(SDMTools) 
#Scalebar(x=47.868,y=-21.375,distance=100,unit='m') #show values in meters 
#Error in map.scale(x = 50, y = -22) : argument "len" is missing, with no default 

map.scale(x=47.868, y=-21.375, ratio=FALSE, relwidth=0.2) 
#Error in map.scale(x = 47.868, y = -21.375, ratio = FALSE, relwidth = 0.2) : 
    unused argument(s) (ratio = FALSE, relwidth = 0.2) 
map.scale(x=47.868, y=-21.375) 
#Error in map.scale(x = 47.868, y = -21.375) : 
    argument "len" is missing, with no default 

我既需要指北针和规模,但也不会情节!为什么?

+0

太谢谢你了!但是如果我需要几公里呢? – user3720904 2014-10-09 15:59:20

回答

1

尝试:

library(GISTools) 
map.scale(53,15,len=12,"Miles",4,0.5,sfcol='red') 
north.arrow(xb=15.75, yb=43.25, len=0.05, lab="N",col="cyan") 

GISTools了解更多详情。

+0

非常感谢,下班后我会尝试。但是如果我需要米而不是英里呢? – user3720904 2014-10-09 15:59:40

+0

只需查看我为GISTools提供的链接。 – Ujjwal 2014-10-09 16:00:28

+0

@ user3720904让我知道这是否有效。 – Ujjwal 2014-10-09 18:29:25

3

ggsn包在这里很有用。如果没有GPS数据,我无法执行所有的脚本,但是在指定的x和y限制的情况下,这应该在合理的位置添加北方符号和比例尺。

#First, add a scale bar. 
n <- n + scalebar(location="bottomright",y.min=-21.3755, y.max=-21.3715, 
      x.min=47.865, x.max=47.869, dist=.1, dd2km= TRUE, model='WGS84', 
      st.dist=.04) 

#Now add a north arrow and plot the map. 
north2(n, x=.3, y=.8, symbol=9) 

north symbol and scale bar example

+0

包装有变化吗?箭头周围有一个黑框。你知道为什么吗? – 2016-09-29 15:05:14

+0

@ M.Beausoleil我只是重新编写了脚本,并没有遇到任何问题,因此我们可能会运行不同版本的行为不同的版本。我用ggsn 0.3.0来做上面的例子。对不起,我不能多帮忙! – 2016-09-30 17:29:03