class: center, middle, inverse, title-slide # The sf package ### Kamal Abdelrahman ### 05-15-2019 --- --- class: left, up # Table of Contents 1. Getting to Know GIS 2. Introducing the sf package 3. Example --- class: left, top # Getting To Know GIS Geographic Information Systems - Framework of capturing, storing, manipulating, analyzing, managing, and presenting spatial data + Features - The geometry of a shape, bits of points that comprise its structure on given place on Earth + Spatial Data - Data identifies geoprahical location + Attribute Data - Data that identifies characteristics of locations + Shapefiles - Geometric shape of spatial data + **NOTE** - MUST BE KEPT IN SAME FOLDER of supporting files + Choropleth - Thematic map with areas of study site shaded in proportion to values of an area --- # Introducing the sf package The sf, short for **simple features**, package allows analysts to process and analyze geospatial data in R, noted by tibbles/data.frames with the **geometry column** * Importing/Exporting shapefiles * Creating choropleths * Works well with ggplot2 syntax --- #Example You're analyzing disparities of income levels across New York City and want to use a choropleth to visualize that ##What will we need? * New York City Shapefile (Community District Level) * New York City Median Incomes (Community District Level) ##How will we actually make one? 1. Read in our shapefile, and dataset 2. Merge them together 3. Visualize the map using ggplot syntax --- # Step 1 - Reading In Shapefile/Data ```r install.packages("sf") ``` ```r library(sf) #Reading From File NycCommunityDistricts <- read_sf("/Users/kamal/Documents/GitHub/FinalPresentation/Community Districts/geo_export_56ffd4c2-465e-4b9e-ab06-8ab9ebaa596f.shp") #Reading From Website NycCommunityDistricts <- read_sf("https://data.cityofnewyork.us/api/geospatial/yfnk-k7r4?method=export&format=GeoJSON") NYCIncome <- fread("https://raw.githubusercontent.com/kamalabdel97/FinalPresentation/master/Median%20Incomes.csv") ``` `read_sf` - Reads in simple features --- # New York Stand Up! <img src="FinalPresentation_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- # Step 2 - Joining the data to the shapefile ```r NycCommunityDistrictsIncome <- merge(NycCommunityDistricts, NYCIncome, by.x = "boro_cd", by.y = "Fips", all.x = T) ``` `NycCommunityDistricts` - The shapefile of the New York City community districts `NYCIncome` - The attribute data contain the income levels --- #Step 3 - Making the choropleth ```r ggplot() + geom_sf(data = NycCommunityDistrictsIncome, aes(fill = Income)) ``` <img src="FinalPresentation_files/figure-html/unnamed-chunk-7-1.png" width="45%" style="float:right; padding:0px" /> `ggplot` - Invokes the ggplot syntax for data visualization `geom_sf` - Visualizes the shapefile feature * `NycCommunityDistrictsIncome` - The shapefile dataframe containing BOTH the shapefile (spatial) and income levels (attribute) * `Income` - The statistical value in the shapefile dataframe for each community district --- #Final Map! ``` ## Warning: Ignoring unknown aesthetics: na.value ``` <img src="FinalPresentation_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- #Let's Connect! Github: [kamalabdel97](http://www.github.com/kamalabdel97) Linkedin: [Kamal Abdelrahman](http://www.linkedin.com/in/KamalAbdel)