Plot on ggmap by using city and state

2019-06-06 23:35发布

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?

2条回答
戒情不戒烟
2楼-- · 2019-06-06 23:50

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.

查看更多
混吃等死
3楼-- · 2019-06-07 00:00

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

查看更多
登录 后发表回答