DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Compute tree density and average tree canopy by neighborhood Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Tree census data and tree canopy data: are they correlated? Tree census vector (points) Tree canopy raster
DataCamp Spatial Analysis with sf and raster in R Create two measures of greenery by neighborhood Tree density by neighborhood Average tree canopy by neighborhood
DataCamp Spatial Analysis with sf and raster in R Compute tree density part 1: counts of trees by neighborhood Count trees by hood: count() from dplyr Drop geometry: set_geometry(geo, NULL) Rename column: rename() from dplyr Use hist() to look at the data
DataCamp Spatial Analysis with sf and raster in R Compute tree density part 2: compute areas and join in counts Use st_area() and mutate() to add areas to the neighborhoods data frame Join in the count data from the previous step with left_join() Finally compute density with counts/area
DataCamp Spatial Analysis with sf and raster in R Tree density final result > head(neighborhoods) # Simple feature collection with 6 features and 8 fields # geometry type: MULTIPOLYGON # dimension: XY # bbox: xmin: -74.00736 ymin: 40.61264 xmax: -73.77574 ... # epsg (SRID): 4326 # proj4string: +proj=longlat +ellps=WGS84 +no_defs # county_fip boro_name hood ntaname boro_code area tree_cnt # 1 047 Brooklyn BK88 Borough Park 3 5017229 565 # 2 081 Queens QN52 East Flushing 4 2736433 295 # 3 081 Queens QN48 Auburndale 4 3173995 507 # 4 081 Queens QN51 Murray Hill 4 4876380 732 # 5 081 Queens QN27 East Elmhurst 4 1832715 211 # 6 005 Bronx BX35 Morrisania-Melrose 2 1569317 214 # tree_density geometry # 1 0.0001126120 MULTIPOLYGON(((-73.97604935... # 2 0.0001078046 MULTIPOLYGON(((-73.79493246... # 3 0.0001597356 MULTIPOLYGON(((-73.77573836... # 4 0.0001501114 MULTIPOLYGON(((-73.80379022... # 5 0.0001151297 MULTIPOLYGON(((-73.86109724... # 6 0.0001363650 MULTIPOLYGON(((-73.89696589...
DataCamp Spatial Analysis with sf and raster in R Steps to compute average tree canopy Make sure that the raster and vector layers have the same CRS Compute mean tree canopy by neighborhood
DataCamp Spatial Analysis with sf and raster in R Tree canopy final result > head(neighborhoods) # Simple feature collection with 6 features and 9 fields # geometry type: MULTIPOLYGON # dimension: XY # bbox: xmin: 1828213 ymin: 2168794 xmax: 1844284 ymax: 2194502 # epsg (SRID): NA # proj4string: +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 ... # county_fip boro_name hood ntaname boro_code area total # 1 047 Brooklyn BK88 Borough Park 3 5017229 565 # 2 081 Queens QN52 East Flushing 4 2736433 295 # 3 081 Queens QN48 Auburndale 4 3173995 507 # 4 081 Queens QN51 Murray Hill 4 4876380 732 # 5 081 Queens QN27 East Elmhurst 4 1832715 211 # 6 005 Bronx BX35 Morrisania-Melrose 2 1569317 214 # tree_density avg_canopy geometry # 1 0.0001126120 0.7253846 MULTIPOLYGON(((1830843.4956... # 2 0.0001078046 8.4133333 MULTIPOLYGON(((1842344.8587... # 3 0.0001597356 4.1568750 MULTIPOLYGON(((1844283.8606... # 4 0.0001501114 8.5205000 MULTIPOLYGON(((1841161.6511... # 5 0.0001151297 2.5930000 MULTIPOLYGON(((1836803.3413... # 6 0.0001363650 3.1740000 MULTIPOLYGON(((1832056.9137...
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Exploratory data analysis with ggplot2 Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Making nice(R) maps ggplot2 for plots and maps tmap for polished maps
DataCamp Spatial Analysis with sf and raster in R Our example data, county area and population # New York State > county <- st_read("county.shp") > head(county) # geoid total_pop area geometry # 1 36101 98665 3601.568 MULTIPOLYGON (((-77.423093 ... # 2 36091 223774 2097.880 MULTIPOLYGON (((-74.124335 ... # 3 36003 48070 2665.875 MULTIPOLYGON (((-78.309193 ... # 4 36075 121183 2464.746 MULTIPOLYGON (((-76.617586 ... # 5 36111 181300 2911.758 MULTIPOLYGON (((-74.67402 4... # 6 36089 112011 6939.259 MULTIPOLYGON (((-74.939447 ...
DataCamp Spatial Analysis with sf and raster in R Set up an empty plot with ggplot() > library(ggplot2) > ggplot(data = county, aes(x = total_pop, y = area))
DataCamp Spatial Analysis with sf and raster in R Examples of the two basic layer types Geometric layers points: geom_point() lines: geom_line() histogram: geom_histogram() map: geom_sf() Statistical layers smooths: stat_smooth() density: stat_density()
DataCamp Spatial Analysis with sf and raster in R Adding layers is easy with the + symbol > ggplot(data = county, aes(x = total_pop, y = area)) + + geom_point()
DataCamp Spatial Analysis with sf and raster in R Adding multiple layers is also easy > ggplot(data = county, aes(x = total_pop, y = area)) + + geom_point() + + stat_smooth()
DataCamp Spatial Analysis with sf and raster in R Create a map with geom_sf() > ggplot(data = county) + + geom_sf()
DataCamp Spatial Analysis with sf and raster in R Use aes() to link the fill color to a variable > ggplot(data = county, aes(fill = area)) + + geom_sf()
DataCamp Spatial Analysis with sf and raster in R Change fill colors with scale_fill_gradient() > ggplot(data = county, aes(fill = area)) + + geom_sf() + + scale_fill_gradient(low = "wheat1", high = "red")
DataCamp Spatial Analysis with sf and raster in R Maps are better with a projected CRS > county <- st_transform(county, crs = 32618) > ggplot(data = county, aes(fill = area)) + + geom_sf() + + scale_fill_gradient(low = "wheat1", high = "red")
DataCamp Spatial Analysis with sf and raster in R Use cor() from base R to compute correlation > cor(county$area, county$total_pop) [1] -0.378985
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Create final, polished maps with tmap Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Initial plot setup function tm_shape() # Running this with no layers will produce an error > tm_shape(county)
DataCamp Spatial Analysis with sf and raster in R tmap layers Polygons: tm_polygons() Image raster: tm_rgb() Lines: tm_lines() Points: either tm_bubbles() or tm_dots() Many others ...
DataCamp Spatial Analysis with sf and raster in R Add layers with a + sign > tm_shape(county) + + tm_polygons()
DataCamp Spatial Analysis with sf and raster in R Add multiple layers (same data) > tm_shape(county) + + tm_polygons() + + tm_bubbles(col = "forestgreen")
DataCamp Spatial Analysis with sf and raster in R Add multiple layers (different data) > tm_shape(elevation) + + tm_raster() + + tm_shape(county) + + tm_borders()
DataCamp Spatial Analysis with sf and raster in R Color based on a variable > tm_shape(county) + + tm_polygons(col = "total_pop")
DataCamp Spatial Analysis with sf and raster in R Multiple maps in one with tmap_arrange() > map1 <- tm_shape(county) + + tm_polygons(col = "total_pop") > map2 <- tm_shape(elevation) + + tm_raster() > tmap_arrange(map1, map2, nrow = 1)
DataCamp Spatial Analysis with sf and raster in R
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R GIS in R - You're ready! Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R GIS with the sf and raster packages sf is a dramatic improvement over sp for handling vectors raster is a smart and intuitive package that can perform spatial analysis with rasters
DataCamp Spatial Analysis with sf and raster in R No problem to read spatial data into R st_read("myvector.shp") raster("myraster.tif") or brick("myraster.tif")
DataCamp Spatial Analysis with sf and raster in R Navigating spatial objects in R Vectors sf objects are data frames Geometry is in list-columns st_geometry() returns just the geometry Rasters Can be single or multi-band Informational functions include extent() , crs() , ncell() , nlayers() Values are left on disk but can be retrieved with getValues()
Recommend
More recommend