Skip to content

Commit 79bcee9

Browse files
committedMay 18, 2018
Update README
1 parent 93fe4a8 commit 79bcee9

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed
 

‎README.Rmd

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: "Intro to GIS for R"
3+
author: "Angela Li"
4+
date: "`r format(Sys.Date())`"
5+
output: github_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE)
10+
```
11+
12+
This talk is a modified version of my [previous talk](https://github.com/angela-li/rladies-spatial-data) for R-Ladies Chicago, with the goal of inspiring UChicago students to go out and try using R for GIS.
13+
14+
I rapidly sailed through a lot of content when I gave the talk, so I'd recommend going back and digging through these slides in depth. You can find the data for the talk in the `data` folder.
15+
16+
Below are the slides from the presentation, which I updated a bit based on feedback I got from y'all. If you have additional comments, please let me know! The shortlink to the slides is [bit.ly/harris-gis](bit.ly/harris-gis) for easy reference.
17+
18+
```{r echo = FALSE}
19+
knitr::include_url('https://angela-li.github.io/slides/2018-05-18/gis-r#1')
20+
```
21+
22+
**Advanced material: If you know how to fork and clone a Github repository, I'd recommend doing that with this repository to get the data quickly. I've extracted all the code out into a R script (using `knitr::purl(gis-R.Rmd)`) that you can simply run once you've forked this repo and cloned it into RStudio on your local device.

‎README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# gis-r
2-
Materials for talk introducing GIS for R for Harris Public Policy students
1+
Intro to GIS for R
2+
================
3+
Angela Li
4+
2018-05-18
5+
6+
This talk is a modified version of my [previous talk](https://github.com/angela-li/rladies-spatial-data) for R-Ladies Chicago, with the goal of inspiring UChicago students to go out and try using R for GIS.
7+
8+
I rapidly sailed through a lot of content when I gave the talk, so I'd recommend going back and digging through these slides in depth. You can find the data for the talk in the `data` folder.
9+
10+
Below are the slides from the presentation, which I updated a bit based on feedback I got from y'all. If you have additional comments, please let me know! The shortlink to the slides is [bit.ly/harris-gis](bit.ly/harris-gis) for easy reference.
11+
12+
[![](README_files/figure-markdown_github/unnamed-chunk-1-1.png)](https://angela-li.github.io/slides/2018-05-18/gis-r#1)
13+
14+
\*\*Advanced material: If you know how to fork and clone a Github repository, I'd recommend doing that with this repository to get the data quickly. I've extracted all the code out into a R script (using `knitr::purl(gis-R.Rmd)`) that you can simply run once you've forked this repo and cloned it into RStudio on your local device.
Loading

‎gis-r.R

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
## ----setup, include=FALSE------------------------------------------------
2+
options(htmltools.dir.version = FALSE)
3+
4+
## ----eval=FALSE, tidy=FALSE----------------------------------------------
5+
## library(tidyverse)
6+
## library(sf)
7+
## library(tmap)
8+
##
9+
## sales <- read_csv("output/sales-tidy.csv")
10+
## tracts <- st_read("data/orig/shapefiles/detroit_tracts.shp")
11+
## tracts <- rename(tracts, tract = GEOID)
12+
##
13+
## sales <- sales %>%
14+
## right_join(tracts, ., by = "tract")
15+
##
16+
## med_sales_map <- tm_shape(sales, unit = "mi") +
17+
## tm_fill("med_price", palette = "Blues", breaks = quantile(a$med_price), title = "Median Sales Price") +
18+
## tm_facets("after_hhf") +
19+
## tm_shape(tracts) +
20+
## tm_borders() +
21+
## tm_compass(fontsize = 0.6, color.dark = "dark grey") +
22+
## tm_scale_bar(color.dark = "dark grey")
23+
##
24+
## save_tmap(med_sales_map, "doc/figs/med_sales_map.png")
25+
26+
## ----eval=FALSE, tidy=FALSE----------------------------------------------
27+
## install.packages("sf")
28+
## install.packages("tmap")
29+
30+
## ----warning=FALSE-------------------------------------------------------
31+
# Load package
32+
library(sf)
33+
34+
# Read in shapefile
35+
chi <- st_read("data/Neighborhoods_2012b.shp")
36+
37+
## ------------------------------------------------------------------------
38+
head(chi)
39+
40+
## ------------------------------------------------------------------------
41+
class(chi)
42+
43+
## ------------------------------------------------------------------------
44+
# Map it using base R: just shape outlines
45+
plot(st_geometry(chi))
46+
47+
## ------------------------------------------------------------------------
48+
# This maps all the attributes
49+
plot(chi)
50+
51+
## ------------------------------------------------------------------------
52+
chi2 <- st_read("data/ComArea_ACS14_f.shp")
53+
54+
## ------------------------------------------------------------------------
55+
# Check what variables we have
56+
names(chi2)
57+
58+
# Calculate population density
59+
library(dplyr)
60+
chi2 <- mutate(chi2, Pop2014 = Pop2014/shape_area)
61+
62+
## ------------------------------------------------------------------------
63+
# Map population density by neighborhood
64+
plot(chi2["Pop2014"])
65+
66+
## ----echo=FALSE----------------------------------------------------------
67+
library(tmap)
68+
tm_shape(chi2) +
69+
tm_fill("Pop2014", palette = "Purples",
70+
title = "Population by Neighborhood, 2014")
71+
72+
## ----warning=FALSE-------------------------------------------------------
73+
groceries <- st_read("data/groceries.shp")
74+
75+
## ----warning=FALSE-------------------------------------------------------
76+
# Get the CRS (coordinate reference system) of the groceries point data
77+
groceries_crs <- st_crs(groceries)
78+
79+
# Project the neighborhood boundaries
80+
chi2 <- st_transform(chi2, groceries_crs)
81+
82+
## ----echo=FALSE----------------------------------------------------------
83+
# Plot both
84+
tm_shape(chi2) +
85+
tm_borders() +
86+
tm_fill("Pop2014", palette = "Purples",
87+
title = "Population by Neighborhood, 2014") +
88+
tm_shape(groceries) +
89+
tm_dots(title = "Groceries", size = 0.1, col = "black")
90+
91+
## ----message=FALSE-------------------------------------------------------
92+
chi2 %>%
93+
st_join(groceries, .) %>%
94+
group_by(community) %>%
95+
tally() %>%
96+
arrange(desc(n))
97+
98+
## ----eval=FALSE----------------------------------------------------------
99+
## get_point_counts_in_buffer <- function(points_to_buffer,
100+
## points_to_intersect,
101+
## buffer_size = 500) {
102+
## number_points_within_buffer <- points_to_buffer %>%
103+
## st_buffer(buffer_size) %>%
104+
## st_contains(points_to_intersect) %>%
105+
## map_dbl(length) %>%
106+
## tibble(pts_in_buffer = .)
107+
##
108+
## return(number_points_within_buffer)
109+
## }
110+

0 commit comments

Comments
 (0)
Please sign in to comment.