在R的谷歌地图上绘制多个点的坐标 [英] Plotting coordinates of multiple points at google map in R

查看:213
本文介绍了在R的谷歌地图上绘制多个点的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数据示例:

 坐标id 
1(7.1735,45.8688)2
2(7.17254,45.8689)3
3(7.17164,45.8692)4 $ b (7.17807,45.8701)6
6(7.17723,45.8692)7 $ b $ 7(7.17524,45.8681)8
8(7.18141,45.8716)5
5(7.17807,45.8701) (7.1793,45.8708)9
9(7.1793,45.8702)10
10(7.17836,45.8707)11 $ b $ 11(7.17519,45.8697)12 $ b $ 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)

#将文本复制到屏幕外
#因为OP没有使用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))

头(数据)
#lon lat id
#1 7.17350 45.8688 2
#2 7.17254 45.8689 3
#3 7.17164 45.8692 4
#etc

#确定一个合理的地图中心,
#这可能会在某些地方失败(接近极点,180次子午线)
#也会谷歌似乎稍微移动一些东西
center = paste (min(data $ lat)+(max(data $ lat)-min(data $ lat))/ 2,
min(data $ lon)+(max(dat一个$ lon)-min(data $ lon))/ 2,sep =)

#从google获得地图图像
map< - get_map(location = center,zoom = 11,maptype =terrain,
source =google)

#开始一个ggplot。它不会绘制直到我们输入p
p < - ggmap(map)

#添加文本标签,它们将重叠
p< - p + geom_text(data = data ,aes(x = lon,y = lat,
label = id),
color =white,size = 4,hjust = 0,vjust = 0)+
主题.position =none)

#最后加点,所以它们位于顶部
p < - p + geom_point(data = data,aes(x = lon,y = lat), color =white,size = 2)

#显示图
p



自然在?get_map ?get_googlemap 中描述。

I wanted to plot the coordinates on a Google Map in the presence of point id:

Sample of data:

          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  

解决方案

Rather than request a map of 'Switzerland' from google, you should request a map of a specific location by specifying a longitude/latitude and desired zoom (and maybe scale). Then you won't have to use coord_map() and blur your image.

Here are the basics, you can play around with colors and sizes as in any 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 

This naturally is described in ?get_map and ?get_googlemap.

这篇关于在R的谷歌地图上绘制多个点的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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