Connecting 2 points in a map using ggplot

ε祈祈猫儿з 提交于 2020-01-11 05:55:08

问题


I have the following code to plot 2 points in ggmap

library(ggmap)
library(ggplot2)

d <- data.frame(lat=c(12.97131,12.98692),
        lon=c(77.5121,77.68627))

Bangalore <- get_map("Bangalore,India", zoom=12)

p <- ggmap(Bangalore)
p + geom_point(data=d, aes(x=lon, y=lat),color="red",size=3)

ggplot(p)

These points are showing as red dots in the map. How can I connect these points?


回答1:


No need for the final ggplot(p) (that's probably throwing an error on your end) and I'd use geom_path:

p <- ggmap(Bangalore)
p <- p + geom_point(data=d, aes(x=lon, y=lat),color="red",size=3)
p + geom_path(data=d, aes(x=lon, y=lat), color="black", size=1)
## or....
p + geom_line(data=d, aes(x=lon, y=lat), color="black", size=1)



来源:https://stackoverflow.com/questions/26572663/connecting-2-points-in-a-map-using-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!