Jonathan Holmes
Jonathan Holmes

Reputation: 13

Add datapoint labels to ggplot2 with giscoR map

I have prepared a map of the UK using giscoR and have successfully added location points to the map using latitude and longitude and ggplot2. I now want to add labels to these points but when I attempt to do so using geom_text, it returns an error message. When I plot the data in ggplot2 but without the addition of the giscoR map, I am able to add labels using the code below.Can anyone advise on how to add labels to the plot with the map included? Many thanks, Jonathan

#Preliminary steps - loading packages etc
library("ggplot2")
library(sf)

#Base map of the study region
library(giscoR)
map <-gisco_get_countries(country = c("UK", "Ireland"))

#Saving location data to a dataframe
data <-LOCATION

LOCATION
   Number Latitude Longitude
1    L-1    53.01     -9.04
2    L-2    53.00     -9.02
3    L-3    52.52     -8.53
4    L-4    54.18     -2.80
5    S-1    54.10     -2.10
6    S-2    54.21     -2.51

#Adding site locations to the basemap - this works
SITEMAP<-ggplot() +
geom_sf(data = map)+
geom_point(data = LOCATION, aes(x = Longitude, y = Latitude))
SITEMAP

#Adding site location lables to the basemap
SITEMAP2<-ggplot() +
geom_sf(data = map)+
geom_point(data = LOCATION, aes(x = Longitude, y = Latitude))+
geom_text(aes(label=Number))
SITEMAP2

#This returns the following error
Error in `geom_text()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error:
! object 'Number' not found

Upvotes: 1

Views: 34

Answers (0)

Related Questions