Plotting coordinates of multiple points at google map in R

后端 未结 2 839
有刺的猬
有刺的猬 2020-12-29 16:54

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.868         


        
2条回答
  •  渐次进展
    2020-12-29 17:37

    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

提交回复
热议问题