简体   繁体   中英

Labeling locations on Google Maps using R

I have a dataframe which has three columns - Places , lat , and long . Places is a list of strings, and the lat and long values are obtained by geocoding the list.

I can plot the lat long values using

map + geom_point(data = lat_long_vec, aes(x = lat_long_vec$lon, y = lat_long_vec$lat), size = 3, colour = "blue",  shape = 19)

where map = map = qmap(location=someplacename, zoom = somezoomvalue)

However, I would like to label the points using the Places column of the dataframe. I have tried the following which hasn't worked.

map + geom_point(data = lat_long_vec, aes(x = lat_long_vec$lon, y = lat_long_vec$lat), size = 3, colour = "blue",  shape = 19) + geom_text(aes(label=lat_long_vec$Places),hjust=0, vjust=0)

Can someone help? Thanks

I would use geom_text like this.

library(ggmap)

mydf <- data.frame(lat = 17.245088, lon = 78.299744, places = c('My place name'))

ggmap(get_googlemap(center = paste(mydf$lat[1], mydf$lon[1]),
                    maptype = 'roadmap',
                    zoom = 10,
                    color = 'bw',
                    scale = 2),
                    extent = 'panel') +
      geom_point(data = mydf,
                 aes(x = lon, y = lat),
                 fill = "red",
                 colour = "black",
                 size = 3,
                 shape = 21) +
      geom_text(data = mydf,
                aes(x = lon,
                    y = lat,
                    label = places),
                color = 'blue')

海得拉巴

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM