I have pieced together some code to make a topography plot using elevation data and ggplot2.
However, now I would like to overlay onto this plot the country outlines, along with points to show the location / elevation of points of interest e.g. cities and also other polygons and lines to show for example routes.
How do I do this?
My code is as follows:
library(dplyr)
library(ggplot2)
library(raster)
library(scales)
library(rgeos)
#get elevation data
dem1<- getData("SRTM",lat=34.5,lon=33)
dem2<- getData("SRTM",lat=34.5,lon=36)
dem <- merge(dem1,dem2)
#create polygon to crop the elevation data file
library(sp)
coords = matrix(c(34, 31,
34, 32.5,
36, 32.5,
36, 31.0,
34, 31),
ncol = 2, byrow = TRUE)
P1 = Polygon(coords)
Ps1 = SpatialPolygons(list(Polygons(list(P1), ID = "a")), proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
#crop the elevation data using the polygon
dem <- crop(dem,Ps1)
#lower the reolution to enable faster plotting
dem_lower_res <- aggregate(dem, fact=10)
dem.p <- rasterToPoints(dem_lower_res )
df <- data.frame(dem.p)
colnames(df) = c("lon", "lat", "alt")
#plot the data
ggplot(df, aes(lon,lat)) +
geom_raster(aes(fill = alt))+
scale_fill_gradientn(colours = terrain.colors(10))