简体   繁体   中英

Adding labels to points plotted on world map in R

I have plotted a series of latitude/longitude coordinates on a World Map using R. I would like to add labels to these points that I have plotted. Currently my code is:

library(maps)
cities<-read.csv("cities.csv", header=T)
cities
                  id  lat   lon
1            Nigeria  7.0   6.0
2             Gambia 13.3  16.0
3           Cambodia 12.0 105.0 
4             France 46.0   2.0  
5             Greece 38.0  23.7

map(database="world")
points(x = cities$lon, y = cities$lat, col = "red", pch=20)

I want to add either a label (cities$id) or number each of the points in order so I know which point corresponds to which of my data entries.

I have seen codes with ggplot2 but I cannot install ggmap (perhaps my version 1.0.44 is too old) so am trying to stay away from this approach.

Any advice would be greatly appreciated. Many thanks in advance!

World map with unlabeled points

Using base R text function, add after the points call:

text(cities$lon, y = cities$lat, cities$id, pos = 4)

You can change the pos to suit: 1 shows text below the point, 2 to the left, 3 above and 4 to the right.

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