Elevation Profiles in R

February 21, 2013
By

(This article was first published on Mollie's Research Blog, and kindly contributed to R-bloggers)

First, let’s load up our data. The data are available in a gist. You can convert your own GPS data to .csv by following the instructions here, using gpsbabel.

`gps <- read.csv("callan.csv",  header = TRUE)`

Next, we can use the function SMA from the package TTR to calculate a moving average of the altitude or elevation data, if we want to smooth out the curve. We can define a constant for the number of data points we want to average to create each moving average value.

If you don’t want to convert meters to feet, a metric version of the code is available in the gist (callanMetric.R).

`library(TTR)movingN <- 5 # define the n for the moving average calculationsgps\$Altitude <- gps\$Altitude * 3.281 # convert m to ftgps\$SMA <- SMA(gps\$Altitude,  n = movingN)gps <- gps[movingN:length(gps\$SMA), ] # remove first n-1 points`

Next, we want to calculate the distance of each point. You can skip this step if your dataset already includes distances.

`library(sp)Dist <- 0for(i in 2:length(gps\$Longitude)) {  Dist[i] = spDistsN1(as.matrix(gps[i,c("Longitude", "Latitude")]),    c(gps\$Longitude[i-1], gps\$Latitude[i-1]),    longlat = TRUE) / 1.609 # longlat so distances will be in km, then divide to convert to miles}gps\$Dist <- DistDistTotal <- 0for(i in 2:length(gps\$Longitude)) {  DistTotal[i] = Dist[i] + DistTotal[i-1]}gps\$DistTotal <- DistTotal`

And finally, we can plot our elevation data using geom_ribbons and ggplot:

`library(ggplot2)ggplot(gps, aes(x = DistTotal)) +geom_ribbon(aes(ymin = 600, # change this to match your min below    ymax = SMA),  fill = "#1B9E77") + # put your altitude variable here if not using moving averageslabs(x = "Miles",  y = "Elevation") +scale_y_continuous(limits = c(600,1200)) # change this to limits appropriate for your region`

 Elevation profile in ggplot2

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...