5

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?

4
  • 1
    Here is a solution using ggplot2: stackoverflow.com/q/22038640/1446289 – Aaron Dec 20 '16 at 14:14
  • 2
    Use text(x, labels = x$columname) – mdsumner Dec 20 '16 at 14:29
  • thanks, that worked fine. Any suggestons to how I can adjust the position of the labels. Right now I have the id numbers of the points right on top of the cross-marker of the points, making it difficult to see. – dtanon Dec 20 '16 at 16:11
  • I figured it out. Added argument "pos" = 1,2,3 or 4 does the job – dtanon Dec 20 '16 at 16:18
4

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

example

4

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)

enter image description here

Note point4 and point8 labels don't overlap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.