簡體   English   中英

在R中的谷歌地圖上繪制多個點的坐標

[英]Plotting coordinates of multiple points at google map in R

我想在點ID的情況下在Google地圖上繪制坐標:

數據樣本:

          coordinates      id      
1   (7.1735, 45.8688)       2    
2  (7.17254, 45.8689)       3     
3  (7.17164, 45.8692)       4    
4  (7.18018, 45.8716)       5    
5  (7.17807, 45.8701)       6     
6  (7.17723, 45.8692)       7    
7  (7.17524, 45.8681)       8     
8  (7.18141, 45.8718)       9     
9   (7.1793, 45.8702)      10     
10 (7.17836, 45.8707)      11     
11 (7.17519, 45.8697)      12     
12 (7.17938, 45.8708)      13     
13 (7.17551, 45.8693)      14    
14 (7.17684, 45.8694)      15     
15 (7.18099, 45.8726)      17     
16 (7.18015, 45.8725)      18     
17 (7.18122, 45.8736)      19     
18 (7.17491, 45.8692)      20     
19 (7.15497, 45.8706)      25    
20  (7.1534, 45.8695)      28     
21 (7.15265, 45.8699)      29    
22   (7.15442, 45.87)      31    
23  (7.1561, 45.8698)      32     
24    (7.184, 45.896)  GSBi_1  
25     (7.36, 45.901) GSBi__1  
26    (7.268, 45.961) GSBj__1  
27    (7.276, 45.836)  GSBj_1 
28    (7.272, 45.899)     GSB  
29 (7.16667, 45.8667)   GSB_r  

您不應從谷歌請求“瑞士”地圖,而應通過指定經度/緯度和所需縮放(可能是縮放)來請求特定位置的地圖。 然后,您將不必使用coord_map()並模糊您的圖像。

以下是基礎知識,你可以在任何ggplot中使用顏色和大小:

library(ggplot2)
library(ggmap)

# copying text off screen
# since the OP did not use dput()
data<-read.table("clipboard")

# reformat
data=data[,-1]
names(data)=c("lon","lat","id")
data$lon <- as.numeric(gsub('[\\(\\)\\,]', '', data$lon))
data$lat <- as.numeric(gsub('[\\(\\)\\,]', '', data$lat))

head(data)
#       lon     lat id   
# 1 7.17350 45.8688  2  
# 2 7.17254 45.8689  3 
# 3 7.17164 45.8692  4 
# etc

# determine a reasonable center for map, 
# this could fail in some places (near poles, 180th meridian)
# also google appears to shift things slightly
 center = paste(min(data$lat)+(max(data$lat)-min(data$lat))/2,
                min(data$lon)+(max(data$lon)-min(data$lon))/2, sep=" ")

# get map image from google
map <- get_map(location = center, zoom = 11, maptype = "terrain", 
       source = "google")

# start a ggplot. it won't plot til we type p
p <- ggmap(map)

# add text labels, these will overlap
p <- p + geom_text(data=data,aes(x = lon, y = lat, 
         label = id),
         colour="white",size=4,hjust=0, vjust=0)+
    theme(legend.position = "none") 

# add points last so they are on top
p <- p + geom_point(data=data,aes(x=lon, y=lat),colour="white",size=2)

# display plot
p 

在此輸入圖像描述

這自然在?get_map?get_googlemap有所描述。

繪制點的一個問題是,如果在函數get_map()使用zoom=10 ,那么你的點在地圖之外並且他們想要繪制,所以我使用zoom=5代替。

    library(ggmap)
    map <- get_map(location = 'Switzerland', zoom = 5, 
        maptype = "terrain",  source = "google") 

對於地圖的繪圖,我使用了函數ggmap() 要添加點,可以使用geom_point() 為此,您的樣本數據保存為數據框df其中包含列xyid 為了更接近點,可以使用coord_map()

    p <- ggmap(map)
    p <- p +geom_point(data=df,aes(x=x,y=y))+
      coord_map(xlim=c(7,8),ylim=c(45.5,46))
    print(p)

如果您需要為每個點添加標簽,請將此行添加到map p

annotate("text",x=df$x,y=df$y,label=df$id)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM