Adding labels to points plotted on world map in R

Eniale picture Eniale · Apr 11, 2018 · Viewed 7.6k times · Source

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

Answer

csgroen picture csgroen · Apr 11, 2018

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.