2014-10-08 138 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