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.csvto 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()andas.character()to explore and manipulate factors. - Use
str(),nrow(),ncol(),dim(),colnames(),rownames(),head(), andtail()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
dplyrpackage to manipulate dataframes. - Use
select()to choose variables from a dataframe. - Use
filter()to choose data based on values. - Use
group_by()andsummarize()to work with subsets of data. - Use
count()andn()to obtain the number of observations in columns. - Use
mutate()to create new variables.
Introduction to Visualization
- Use
ggplot2to create plots. - Think about graphics in layers: aesthetics, geometry, etc.
Writing Data
- Save plots using
ggsave(). - Use
write.csvto save tabular data.
Intro to Raster Data
- The GeoTIFF file format includes metadata about the raster data.
- To plot raster data with the
ggplot2package, 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()andcut(). - Use built-in
terrain.colors()or set your preferred color scheme manually. - Layer rasters on top of one another by using the
alphaaesthetic.
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
ggplotusing thegeom_sf()function. No need to convert to a dataframe.
Explore and Plot by Vector Layer Attributes
- Spatial objects in
sfare 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
ggplot2package.
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.
tmapdoes this best (see Challenge 2)! - Use the
show.legendargument to set legend symbol types. - Use the
scale_fill_manual()function to set legend colors.
Handling Spatial Projection & CRS
-
ggplot2automatically 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
sfobject using thest_as_sf()function. - Export an
sfobject as text using thest_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 thepaste()function to add to your current text.