Plotting coordinates of multiple points at google map in R
Asked Answered
Y

2

3

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  
Yonder answered 30/12, 2012 at 22:48 Comment(5)
So, what is your problem?Tena
@Tena I do not know how I can automatically get the plot of points on Google maps.Yonder
Have you looked at this #13421200 ?Dysthymia
@MattBagg, yes, but I cannot add the coordinates with labels to the map. I retrieved the map from Google map [map <- get_map(location = 'Switzerland', zoom = 10, maptype = "terrain", + source = "google")] but I do not know how I can add coordinates to map with labels.Yonder
Can you plot the points on the map and only need labels? I suggest you edit the question to include the code you have so far.Dysthymia
D
8

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 

enter image description here

This naturally is described in ?get_map and ?get_googlemap.

Dysthymia answered 1/1, 2013 at 1:55 Comment(5)
I faced with an error in last plot as: Don't know how to automatically pick scale for object of type standardGeneric. Defaulting to continuous Error in data.frame(x = c(7.1735, 7.17254, 7.171636, 7.18018, 7.17807, : arguments imply differing number of rows: 29, 0Yonder
It is hard to say what caused that without access to your code and data. What version of ggplot2 does sessionInfo() indicate? If you str() your dataframe, what are the types of data in it?Dysthymia
Sorry, I made a mistake. The version is ggplot2_0.9.3 and 'data.frame': 29 obs. of 1 variable: $ id.X.y.z: chr "2,7.1735,45.86880001,0" "3,7.17254,45.86887001,0" "4,7.171636,45.86923601,0" "5,7.18018,45.87158001,0" ...Yonder
All your data are in a single column/variable stored as a string. You need each variable to be stored separately as numbers. You should probably rewrite your method of loading/getting the data to parse it properly (or, less ideally, use a combination of as.numeric() and strsplit() to create separate variables that are numeric).Dysthymia
I modified the data as seprate columns and numeric but still I faced with the same error: 'data.frame': 29 obs. of 4 variables. Data: $ id : Factor w/ 29 levels "10","11","12",..: 10 15 18 19 20 21 22 23 1 2 ... $ lon: num 7.17 7.17 7.17 7.18 $ lat: num 45.9 45.9 45.9 45.9 $ NA : num 0 0 0 0 0 0 0 0 0 Error: Don't know how to automatically pick scale for object of type standardGeneric. Defaulting to continuous Error in data.frame(x = c(7.1735, 7.17254, 7.171636, 7.18018, 7.17807, : arguments imply differing number of rows: 29, 0Yonder
A
2

One of the problems with plotting of your points is that if you use zoom=10 in function get_map() then your points are outside the map and they want be plotted, so I used zoom=5 instead.

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

For the plotting of map I used function ggmap(). To add points geom_point() can be used. For this purpose your sample data were saved as data frame df with columns x, y and id. To zoom closer to points coord_map() can be used.

    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)

If you need to add labels to each point then add this line to map p

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

enter image description here

Apriorism answered 31/12, 2012 at 8:4 Comment(2)
As you can see, the short distance of point cause unbeautifully presentation. Do u have any idea to enhance the plot?Yonder
@aliamidi one solution would be to try to get map (other than google) with higher resolution and use it as background.Apriorism

© 2022 - 2024 — McMap. All rights reserved.