3

I am a complete noob at using GIS in R and have been trying many packages and functions but get stuck each time. What I want is to generate an elevation map for a location in the Himalayas with additional geographic points that are labeled, and possibly rivers.

What I have so far is the following code that retrieves elevation data and does some basic plotting:

    library(elevatr)
    library(raster)

    #set  projection
    prj4<-"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

    # set plot bounds
    bounds <- data.frame(x=c(85.8,87.4),y=c(26.6,28.2))

    # retrieve elevation raster
    elev<-get_elev_raster(locations = bounds, 
                          prj = prj4,
                          z=9, 
                          clip="locations")

    plot(elev)

    # Add two dots
    dotcoords<-data.frame(x = c(86.3,87.1), y = c(27.2,27.8))
    rownames(dotcoords)<-c("locA","locB")
    coordinates(dotcoords) <- c("x","y")
    proj4string(dotcoords) <- CRS("+proj=longlat +datum=WGS84") 
    pts_dotcoords <- spTransform(dotcoords, CRS(prj4))

    plot(pts_dotcoords, add=T, pch=16)

This generates the following plot:elevation_plot My question is how I can add labels to the dots showing the associated names ("locA" and "locB")?

This is probably basic stuff but I really need some advice on what approach to take to get me going in the right direction (would it be better to use ggplot2 or ggmap, for example?).

1
  • Thanks for your directions. I have edited my post accordingly
    – RvV
    Jan 25 '20 at 13:48
2

After plotting the the points you need to call text():

plot(elev)
plot(pts_dotcoords, add = TRUE, pch = 16)    
text(pts_dotcoords, labels = c("locA", "locB"), pos = 4, offset = 0.7)

R plot

See also ?text:

pos: a position specifier for the text. If specified this overrides any adj value given. Values of ‘1’, ‘2’, ‘3’ and ‘4’, respectively indicate positions below, to the left of, above and to the right of the specified coordinates.

offset: when pos is specified, this value gives the offset of the label from the specified coordinate in fractions of a character width.

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.