--- title: "RMarkdown Tutorial Using Rasters" author: "Billy Raseman" date: "February 10, 2017" output: html_document --- ## Overview: This is an RMarkdown file to present Raster data. Using RMarkdown and knitr, it is easy to create easy and elegant code projects in R. Markdown is not just limited to R, in fact, it is widely used across languages. If you are interested in these types of tools, also check out Jupyter Notebooks as well. ### Clear environment ```{r} rm(list=ls()) ``` ### Load packages ```{r, message=FALSE, warning=FALSE} library(raster) library(rasterVis) library(rgl) library(rgdal) library(ggplot2) ``` ### Set working directory to data folder ```{r} # MUST CHANGE THIS LINE OF CODE TO MATCH YOUR DIRECTORY SET-UP! setwd("C:/Users/Billy/Google Drive/CU Boulder Courses/CVEN 5454 Statistics and Probability/RMarkdown_CVEN-5454/scripts/") ``` # Raster Tutorial: source: http://neondataskills.org/R/Raster-Data-In-R/ ### Open the raster files ```{r} mask = raster("../data/mask.asc") dem = raster("../data/dem.asc") ``` ### Get information on the rasters and determine coordinate system ```{r} mask dem mask@crs dem@crs ``` ### Plot initial images ```{r, fig.height=4, fig.width=5} image(mask) image(dem) ``` ### Change the colors ```{r, fig.height=4, fig.width=5} col1=topo.colors(5) image(dem,col=col1) ``` ### Add more colors ```{r, fig.height=4, fig.width=5} col2=topo.colors(10) image(dem,col=col2) ``` ### Change the color scheme ```{r, fig.height=4, fig.width=5} col3=terrain.colors(10) image(dem,col=col3) ``` ### Plot the histogram ```{r, fig.height=4, fig.width=5} hist(dem, main="Distribution of elevation values", col= "purple", maxpixels=21752940) ``` ### Crop the raster to the mask, then plot the new cropped raster ```{r} dem_crop = dem*mask image(dem_crop,col=col3) ``` ## Plot using ggplot() ### Convert raster to points and create dataframe ```{r} dem.p <- rasterToPoints(dem_crop) df.p <- data.frame(dem.p) colnames(df.p) <- c('Easting', 'Northing', 'Elev') # change the column names of dataframe ``` ### Plot! ```{r, fig.height=5} ggplot(data=df.p, aes(y=Northing, x=Easting)) + geom_raster(aes(fill=Elev)) ```