BootcampR
AN INTRODUCTION TO R
Jason A. Heppler, PhD University of Nebraska at Omaha March 17, 2020 @jaheppler
BootcampR AN INTRODUCTION TO R Jason A. Heppler, PhD University of - - PowerPoint PPT Presentation
BootcampR AN INTRODUCTION TO R Jason A. Heppler, PhD University of Nebraska at Omaha March 17, 2020 @jaheppler Hi. I'm Jason. I like to gesture at screens. Digital Engagement Librarian , University of Nebraska at Omaha Mentor, Mozilla Open
AN INTRODUCTION TO R
Jason A. Heppler, PhD University of Nebraska at Omaha March 17, 2020 @jaheppler
I like to gesture at screens.
Digital Engagement Librarian, University of Nebraska at Omaha Mentor, Mozilla Open Leaders Researcher, Humanities+Design, Stanford University
Due to COVID-19 closures, this will be the final workshop. Open up RStudio. We'll start doing a few things together soon.
between spatial data and other data types
geometries stored in list columns
We have to know about a few different data types in GIS before we go much further. Vector data: representing real world features like hills, roads, houses, rivers. Vectors have attributes that consist of text or numeric information that describe features. Vectors are often shapes created through geometry. Raster data: a matrix of pixels containing values that represent conditions for an area. This could be background images of the Earth's surface or continuous data represented
sf objects have a hierarchical structure composed of three classes:
attribute column and one geometry column
library(tidyverse) library(sf) churches <- read_csv("https:// raw.githubusercontent.com/unolibraries/workshops/ master/bootcampr/data/churches.csv")
# Let's look at the data structure. glimpse(churches)
Observations: 786 Variables: 13 $ Congregation <chr> "Adventist", "Adventist", "Adventist", "Assemblies of God, Ge… $ National_Affiliation <chr> "Seventh-Day Adventist Denomination", "Seventh-Day Adventist … $ Sub_Affiliations <chr> "General Conference of Seventh-day Adventists; Southwestern U… $ Name <chr> "First Italian Seventh-day Adventist Church", "New Orleans Se… $ Building_Type <chr> "Church", "Church", "Church", "Church", "Church", "Church", "… $ Congregation_Race <chr> "Italian", "White", "Black", "White", "White", "White", "Whit… $ Cross_Street <chr> "full address provided", "full address provided", "full addre… $ Street_Address <chr> "1004 Kerlerec St.", "3500 St. Charles Ave.", "2412 Delachais… $ City_State <chr> "New Orleans, Louisiana", "New Orleans, Louisiana", "New Orle… $ Zip_Code <dbl> 70116, 70115, 70115, 70032, 70114, 70124, 70118, 70119, 70130… $ `Full Address` <chr> "1004 Kerlerec St. New Orleans, Louisiana 70116", "3500 St. C… $ Latitude <dbl> 29.96598, 29.92736, 29.93523, 29.95605, 29.93741, 29.98737, 2… $ Longitude <dbl> -90.06210, -90.09289, -90.09643, -90.00180, -90.04852, -90.11…
Head over to RStudio and give this a shot. churches %>% ggplot() + geom_point(aes(x = Longitude, y = Latitude)) + labs(title="Churches in New Orleans")
Head over to RStudio and give this a shot. churches %>% ggplot() + geom_point(aes(x = Longitude, y = Latitude)) + labs(title="Churches in New Orleans") Your turn:
basemap <- get_stamenmap(bbox = c(left = min(churches$Longitude), right = max(churches$Longitude), bottom = min(churches$Latitude), top = max(churches$Latitude)), zoom = 12)
To make our map a bit better, we'll use the ggmap package to find a basemap to put underneath the vector layer. The package makes this super easy for us.
To make our map a bit better, we'll use the ggmap package to find a basemap to put underneath the vector layer. The package makes this super easy for us.
basemap <- get_stamenmap(bbox = c(left = min(churches$Longitude), right = max(churches$Longitude), bottom = min(churches$Latitude), top = max(churches$Latitude)), zoom = 12)
get_stamenmap returns a raster that we can now plot. To do this, however, we need to use the ggmap function, which can also accept ggplot's geoms.
Homework for you: what is bbox? Can you figure out what's happening in the code above?
Your turn!
ggmap(basemap) How would you add the church data to this?
coloring the points by congregation again.
(title, subtitle, and remove legend).
Let's look at our data another way. A key history of American cities in the 1930s and 1940s is racial segregation. We can plot the race of the congregations in our dataset to start exploring this. Try this out: basing your work off the previous code we wrote, change the parameters of the data to color points by the Congregation_Race column.
Leaflet is a popular open-source JavaScript library for making interactive maps. The leaflet package allows us to easily integrate and control Leaflet maps in R. install.packages("leaflet") library(leaflet)
Let's start with a simple Leaflet map and break down what's happening.
leaflet() %>% # we call the leaflet function addTiles() # add provider tiles, by default OSM
Let's jump over to RStudio and try it out.
Leaflet works somewhat similarly to ggplot. We can pass in data to the leaflet() function, we can pipe to other functions, and we use Leaflet's grammar of maps to build our visualization. Jump over to RStudio and give this a try.
leaflet() %>% addProviderTiles() %>% addCircleMarkers()
Your turn! Start with the code below.
(Hint: ?addCircleMarkers)
leaflet() %>% addProviderTiles() %>% addCircleMarkers()
Adding a palette to Leaflet maps isn't quite as simple as ggplot's methods of doing so. However, Leaflet comes with wrappers to make the process a bit easier. colorNumeric(): For shading continuous values colorBin(): Continuous values, discrete colors colorQuantile(): Continuous values, discrete colors colorFactor(): For categorical data To use these, we'll create a palette function to apply to the data.
Since we're looking at the race of the congregations, we'll use the colorFactor() function to generate a palette based off our data. Each of the palette functions accept two common parameters
You can pass NULL to have the range inferred from the data.
Since we're looking at the race of the congregations, we'll use the colorFactor() function to generate a palette based off our data. pal <- colorFactor(palette, domain) Let's head over to RStudio and walk through what's happening.
In addition to our point data of church locations, we can also read in and map shapefiles. Shapefiles are a popular GIS vector data format that can contain polygons, points, and lines that is regulated by Esri. To do this next part, visit the workshop Github page below and download the holc.zip file. Put all of these files into a single folder. https://github.com/unolibraries/workshops/ tree/master/bootcampr/data
To get this data in, we'll use the sf function st_read(). holc <- st_read("holc.shp") Let's jump over to RStudio.
Next workshop: March 31, 1:30p-3p: Clustering and Classifying (CL 112)