Julia G. de Aledo
  1. /

Elevation profile of Spain in R

From Picos de Europa to Sierra Nevada #

Have you ever wondered what would be of Spain if you cut it in half? What is there after Despeñaperros? Which is taller, meseta norte or meseta sur? How come Ávila is the highest capital province in Spain?!

I did, and here is the result:

profile

Indeed, after Despeñaperros, there is a big hole.

Here are some tips to your elevation profile in R

Step by step #

  1. Download DEM (e.g. for Spain: IGN)

  2. Download line shapefile with the trajectory of your profile (e.g. from GIS)

  3. Download world map contour countries

  4. Load all to R

  5. Convert line to points

     # Obtain line elevation values
     line2 <- spTransform(line, "+init=epsg:25830")
     psp <- as.psp.SpatialLines(line2)
     pts <- pointsOnLines(psp, eps=10)
     # Convert to spatial points
     pts1 <- SpatialPoints(coords = cbind(pts$x, pts$y), proj4string = CRS("+init=epsg:25830"))
     pdf_data <- as.data.frame(pts1)
    
  6. Calculate elevation and distance

    pdf_data$dem <- raster::extract(r0, pts1) 
    pdf_data$dist <- seq(0, (nrow(pdf_data)-1)*10, 10)
    pdf_data$dist <- pdf_data$dist/1000 # to km
    
  7. Plot it with ggplot

    • geom_line with MetPalettes color Hokusai

    • geom_label_repel to add the labels

    • theme_solarized for background color

    • geom_polygon to plot the Spain contour

    • inset_element to add it to the previous graph

You can find the full code here