This and other labs for this class are based on those developed by C.J. Gabbe and Iris Stewart-Frey
Create a ‘Module2_Lab’ folder in the drive of your choice (preferably somewhere that’s backed up into the cloud) but you can also do your z drive
Create a new RScript by going to File > New File > RScript. Save the RScript to your ‘Module2_Lab’ folder.
It is good professional practice to create a header for your script. That way you will have important information at your fingertips when you are revisiting our sharing your work. Create a header for your script that includes a brief description, your name, and date based on the example below:
# Purpose: This script loads the Cal Enviroscreen shapefile and his Lab 2 of ENVS 117
# Author: Jake Dialesandro
# Date: 03/07/2024
You only have to install packages once (I don’t save the
install.packages code in my RScript – and it’s good coding form to leave
that out). Install readxl, which we’re going to need to
read from an Excel file, and install ggplot which we will use later for
our making graphics:
install.packages("readxl")
install.packages("ggplot")
But, you have to load packages every time you start a new R session. I recommended including a section at the top of your RScript that loads all of the packages. That way you can make sure to load them all at one time.
library("tidyverse")
library("sf")
library("readxl")
library("ggplot2")
We installed tidyverse and sf last time,
right? You can always check to see what packages are installed by
typing:
installed.packages()
How many packages do you have installed? Try Googling one of them to see what it does.
Your working directory provides a shortcut to tell the software where to get and save files. We’re going to choose the Lab 2 folder that you created a few minutes ago. For example, here’s the folder I’m working from today, notice mine is in the cloud on google drive but you may also work on your laptops drive:
setwd("/Users/johndialesandro/Library/CloudStorage/GoogleDrive-jdialesandro@scu.edu/My Drive/ENVS117/Labs/Module2_Lab")
or
#setwd("~/Desktop/ENVS117/Module2_lab")
Again, you can check your working directory by typing
getwd() into the console.
Read the state’s Drinking Water - SAFER Dashboard Failing and At-Risk Drinking Water Systems data.
CA_SAFER <- read_csv("Drinking_Water_Risk_Assessment.CSV.csv")
Ignore any warnings you may get from reading the data in. Describe a few of the available that you see inside of the dataset you just read in? Lets load another csv file in from Affordable houisng and sustainable communities
Read the state’s Affordable Housing and Sustainable Communities 2015 data.
CA_AHSC <- read_csv("https://data.ca.gov/dataset/d8b4bef2-ff64-4908-8c40-65bb1f265b8f/resource/6618d37e-dd4f-480e-8160-2f1ab8285815/download/2015-ca-affordable-housing-and-sustainable-communities-ahsc-awards.csv")
Download the SWS Risk file from here
and save to your Module 2 lab folder. We’re using the
readxl package to open an Excel file with data on the risk
factors of small water systems throughout the state of California. More
infomration can be found from here
SWS_RISK <- read_excel("SWSrisk.xlsx")
This is similar to the read.csv functions we have used previously and you may be used to do doing. If you want to learn more about the syntax for any function (call), type a question mark before it:
?read_excel
Where does the help screen load? What does it tell you?
Download and unzip the CalEnviroScreen shapefile to your Module 2 lab folder.
There are two main packages for working with vector data:
sf and sp. However, sp is in the
process of getting phased out (though now and then you’ll still come
across packages that require it). We’re mainly going to use
sf in this course (sf stands for “simple features”). You
can read more about it here.
Let’s load the shapefile:
CalEnviro <- st_read("CES4 Final Shapefile.shp")
I highly recommend using headers (comments with the # symbol) to organize your code. Try adding these headers to your RScript and re-organize your code under the appropriate header:
# Set-up to load packages and set working directory
# AHSC
# Employer-provided housing
# CalEnviroScreen
There are a number of different ways to browse and explore a new dataset. Try using all of these on the CalEnviroScreen data:
names(CalEnviro)
view(CalEnviro)
summary(CalEnviro)
head(CalEnviro)
tail(CalEnviro)
plot(CalEnviro) # Check the 'Plots' tab in the lower right pane; this will produce plots for the first 10 variables.
What variables are in the dataset? What does each command do?
What is the definition of a numeric variable? A string or character variable? A factor variable? What is an example of each from the data you’ve loaded? There’s a good basic explanation here.
Sometime variables are in the wrong format for our proposes. Like a variable is formatted as a number but it should be text (remember when you did joins with Census IDs in ENVS 116?). Other times a variable is formatted as text but it should be numeric. This is important because you can’t do calculations on numbers that are formatted as text. Here’s how you can convert variables to the proper format:
CalEnviro$ZIP <- as.character(CalEnviro$ZIP) # This converts the zip codes in CES to text, so you could summarize the data more easily by zip code
Try explaining what the code above does in plain English…
You can use similar notation using as.numeric to convert
to numeric or as.factor to convert to factor.
A trickier example is in the CA_AHSC data. Notice that the grant
dollar variables, like CA_AHSC$Affordable, include a dollar sign and are
thus formatted as text. We may want to create a numeric variable so we
can add, subtract, etc. We’ll use parse_number from the
readr package. You can check to see if readr
is installed by typing installed.packages("readr"). If it’s
not installed, please install it. (And then load it.)
CA_AHSC$`Total Awarded` <- parse_number(CA_AHSC$`Total Awarded`)
Now check the format of the Total Awarded variable. What are the two changes that happened?
Let’s say we want to calculate a new variable for our dataset. It’s
easy! Let’s say we want to calculate a variable for number of children
per Census tract in our CalEnviroScreen data. We have
CalEnviro$TotPop19 showing us the population in 2019, and
we have CalEnviro$Child_10 showing us the percentage of
children in the tract (though note that we need to divide by 100). We’ll
add a new variable that we’ll call ‘Child19’:
CalEnviro$Child19 <- CalEnviro$TotPop19 * (CalEnviro$Child_10 / 100)
FYI, we’ll do this same thing a bit later in the course using
mutate, which is from the Tidyverse.
Now lets use our old friend ggplot to help make maps. GGplot which stands for Graphics Grammar Plot will allow us to make some nicer figures. let’s plot PovertyP. We will have to remove the “#” to run these lines of code
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.3.2
#ggplot() +
# geom_sf(data = CalEnviro, aes(fill = PovertyP)) +
#scale_y_continuous(breaks = 0:100)
Notice that the scale is off and it is because we have some nodata values represented as “-999” we are going to filter those values out and plot again! in R language not equal to can be portrayed by the != symbol. We will have to remove the “#” to run these lines of code
#CalEnviro <-filter(CalEnviro, PovertyP != -999)
#ggplot() +
# geom_sf(data = CalEnviro, aes(fill = PovertyP)) +
#scale_y_continuous(breaks = 0:100)
And just like that, our data has been mapped out without the no data values messing with the output legend.
Save the RScript you’ve been working with (I recommend saving along the way too, just in case the program freezes or crashes). Then, find a shapefile online (anything that interests you!). Import the shapefile into R and explore the data in your shapefile. What variables are present? What is the format of these variables? Try out the different commands for summarizing (see #7 above). I’d like you to submit these things to Camino: