|
| 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