I have plotted a shapefile containing points in R, and I would like to add labels like : point 1, point 2 and so on..) to the plot.
Is this possible?
You can try a simple reproducible example below:
# Libraries for GIS
library('sp')
# Library for plot
library('latticeExtra')
# Load data ---------------------------------------------------------------
# Points
x <- c(1199999, 1080000, 1093067, 1090190, 1087977, 1070419, 1180419)
y <- c(957803,937803, 894366, 872153, 853703, 825353, 805353)
# Create SpatialPoints
SP <- SpatialPoints(coords = cbind(x,y))
# Add label variable
SP$ID <- paste("point", 1:7, sep = "")
# Plot
plot <- spplot(SP, zcol = "ID")
labels <- layer(sp.text(coordinates(SP), txt = SP$ID, pos = 1))
# plot + label layer
plot + labels
For base graphics, use pointLabel
from maptools
package.
Using this data:
x <- c(1199999, 1080000, 1093067, 1090190, 1087977, 1070419, 1180419)
y <- c(957803,937803, 894366, 872153, 853703, 825353, 805353)
Add another point close to point 4:
library(sp)
x = c(x,x[4]+1000)
y = c(y,y[4])
SP <- SpatialPoints(coords = cbind(x,y))
SP$ID <- paste("point", 1:length(SP), sep = "")
Plot points, add labels:
library(maptools)
plot(SP)
pointLabel(coordinates(SP),labels=SP$ID)
Note point4 and point8 labels don't overlap.
ggplot2
: stackoverflow.com/q/22038640/1446289