Introduction to R and RStudio


  • Use RStudio to write and run R programs.
  • R has the usual arithmetic operators.
  • Use <- to assign values to variables.
  • Use install.packages() to install packages (libraries).

Project Management With RStudio


  • Use RStudio to create and manage projects with consistent layout.
  • Treat raw data as read-only.
  • Treat generated output as disposable.

Data Structures


  • Use read.csv to read tabular data in R.
  • The basic data types in R are double, integer, complex, logical, and character.
  • Use factors to represent categories in R.

Exploring Data Frames


  • Use cbind() to add a new column to a data frame.
  • Use rbind() to add a new row to a data frame.
  • Use levels() and as.character() to explore and manipulate factors.
  • Use str(), nrow(), ncol(), dim(), colnames(), rownames(), head(), and tail() to understand the structure of a data frame.
  • Read in a csv file using read.csv().
  • Understand what length() of a data frame represents.

Subsetting Data


  • Indexing in R starts at 1, not 0.
  • Access individual values by location using [].
  • Access slices of data using [low:high].
  • Access arbitrary sets of data using [c(...)].
  • Use logical operations and logical vectors to access subsets of data.

Data frame Manipulation with dplyr


  • Use the dplyr package to manipulate dataframes.
  • Use select() to choose variables from a dataframe.
  • Use filter() to choose data based on values.
  • Use group_by() and summarize() to work with subsets of data.
  • Use count() and n() to obtain the number of observations in columns.
  • Use mutate() to create new variables.

Introduction to Visualization


  • Use ggplot2 to create plots.
  • Think about graphics in layers: aesthetics, geometry, etc.

Writing Data


  • Save plots using ggsave().
  • Use write.csv to save tabular data.

Intro to Raster Data


  • The GeoTIFF file format includes metadata about the raster data.
  • To plot raster data with the ggplot2 package, we need to convert it to a dataframe.
  • R stores CRS information in the Proj4 format.
  • Be careful when dealing with missing or bad data values.

Plot Raster Data


  • Continuous data ranges can be grouped into categories using mutate() and cut().
  • Use built-in terrain.colors() or set your preferred color scheme manually.
  • Layer rasters on top of one another by using the alpha aesthetic.

Reproject Raster Data


  • In order to plot two raster data sets together, they must be in the same CRS.
  • Use the project() function to convert between CRSs.

Open and Plot Vector Layers


  • Metadata for vector layers include geometry type, CRS, and extent.
  • Load spatial objects into R with the st_read() function.
  • Spatial objects can be plotted directly with ggplot using the geom_sf() function. No need to convert to a dataframe.

Explore and Plot by Vector Layer Attributes


  • Spatial objects in sf are similar to standard data frames and can be manipulated using the same functions.
  • Almost any feature of a plot can be customized using the various functions and options in the ggplot2 package.

Plot Multiple Vector and Raster Layers


  • Use the + operator to add multiple layers to a ggplot.
  • Multi-layered plots can combine raster and vector datasets. tmap does this best (see Challenge 2)!
  • Use the show.legend argument to set legend symbol types.
  • Use the scale_fill_manual() function to set legend colors.

Handling Spatial Projection & CRS


  • ggplot2 automatically converts all objects in a plot to the same CRS.
  • Still be aware of the CRS and extent for each object.

Convert from .csv to a Vector Layer


  • Know the projection (if any) of your point data prior to converting to a spatial object.
  • Convert a data frame to an sf object using the st_as_sf() function.
  • Export an sf object as text using the st_write() function.

Manipulate Raster Data


  • Use the crop() function to crop a raster object.
  • Use the extract() function to extract pixels from a raster object that fall within a particular extent boundary.
  • Use the ext() function to define an extent.

Create Publication-quality Graphics


  • Use the theme_void() function for a clean background to your plot.
  • Use the element_text() function to adjust text size, font, and position.
  • Use the brewer.pal() function to create a custom color palette.
  • Use the gsub() function to do pattern matching and replacement in text and the paste() function to add to your current text.