Plot on ggmap by using city and state

余生颓废 提交于 2019-12-08 06:56:25

问题


I have a map of the USA, ready to go:

mygggmap <- get_map(location = "United States", zoom = 4) g <- ggmap(myggmap)

The next step is to plot some major cities.

I understand I can create a layer to plot my major cities by their longitude and latitude, something like:
g <- g + geom_density2d(data = citydata, aes(x = lon, y = lat))

but I am wondering if there is a way to plot by the actual name, city and state? Since ggmap is layered over the Google Map API, this should be possible, correct?


回答1:


I found the answer. It is simple. Use the geocode function:

geocode("SEATTLE")

returns

        lon        lat
     -122.3321   47.60621

Thanks for the support everyone. Even though I just got a 'tumbleweed' badge.




回答2:


If you want to explore further, you could also supplement your original data with data from the zipcode package. It has zip codes for about 43K US cities. For more information, you can check out the link http://www.r-bloggers.com/my-first-r-package-zipcode



来源:https://stackoverflow.com/questions/22752009/plot-on-ggmap-by-using-city-and-state

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