

Hill.raster <- hillShade(slope.raster, aspect.raster, 40, 270)Ĭolnames(hill.df) <- c("lon", "lat", "hill") (Thanks to tim riffe for this) slope.raster <- terrain(dem.raster, opt='slope')Īspect.raster <- terrain(dem.raster, opt='aspect') I then transform the resulting hill.raster into a ame as before. Then, I compute the hill shade with raster::hillShade(), setting the elevation angle (of the sun) to 40 and the direction angle of the light to 270. First, I extract the terrain characteristics (the slope and the aspect in this case) with raster::terrain(). Now, instead of colouring each cell of the raster based on its altitude, I want to convey the same information with the hill shade. Scale_fill_gradientn(colours = lors(100)) +Īes(x=long, y = lat, group = group), fill = NA, colour = 'black') + Geom_raster(data = dem.df, aes(lon, lat, fill = alt), alpha =. I can already plot dem.df with ggplot() +

dem.m <- rasterToPoints(dem.raster)Ĭolnames(dem.df) = c("lon", "lat", "alt") The third column of the dem.df contains the altitude value ( alt). dem.raster <- crop(dem.raster, as(my_bbox_buff_25000.sf, 'Spatial'), snap='out')īecause I need to add my raster as a ggplot layer, I need to transform it first into a matrix of points with raster::rasterToPoints() and finally into a ame object. I need to convert my bbox first with as(my_bbox_buff_25000.sf, 'Spatial'). The function raster::crop() takes an sp object (form the sp package) to be used as mask to determine the extent of the resulting (cropped) raster. The SRTM data file is actually pretty large so it probably a good idea to crop it to reduce plotting time. The arguments lat and lon are used to get the data file that is needed for the project.
#Jmap altitude geometry download#
The package raster ships with the function ( raster::getData()) to download the data directly from the R console. There are different options, I will use here data obtained by the Shuttle Radar Topography Mission (SRTM). To map the elevation around our points, I need to download data of a Digital Elevation Model (DEM).

Elevation differences (that is, hills, mountains and valleys) are crucial constraints in the development of human and animal activities. coord_sf allows to set the limits of the plot which are extracted from the bounding box of my_bbox_buff_25000.sf with the function sf::st_bbox().ĭepending on the specific geography of the place you need to map, it might be helpful to provide an idea of the elevation. To give some geographic context to the data I plotted also the country polygons of Italy and Switzerland from the ggplot2::map_data('world'). This is how my point data look like in its essence. St_buffer(dist = 5000) %>% # 5 kilometers Let’s first start with some data: a few points somewhere in Italy.
