introducing sp objects
play

Introducing sp objects Working with Geospatial Data in R Data - PowerPoint PPT Presentation

Working with Geospatial Data in R Introducing sp objects Working with Geospatial Data in R Data frames arent a great way to store spatial data > head(ward_sales) ward lon lat group order num_sales avg_price 1 1 -123.3128


  1. Working with Geospatial Data in R Introducing sp objects

  2. Working with Geospatial Data in R Data frames aren’t a great way to store spatial data > head(ward_sales) ward lon lat group order num_sales avg_price 1 1 -123.3128 44.56531 0.1 1 159 311626.9 2 1 -123.3122 44.56531 0.1 2 159 311626.9 3 1 -123.3121 44.56531 0.1 3 159 311626.9 ... 4 1 -123.3119 44.56531 0.1 4 159 311626.9 5 1 -123.3119 44.56485 0.1 5 159 311626.9 6 1 -123.3119 44.56430 0.1 6 159 311626.9 > nrow(ward_sales) [1] 4189 ● No easy way to keep coordinate reference system information

  3. Working with Geospatial Data in R Data frames aren’t a great way to store spatial data > head(ward_sales) ward lon lat group order num_sales avg_price 1 1 -123.3128 44.56531 0.1 1 159 311626.9 2 1 -123.3122 44.56531 0.1 2 159 311626.9 3 1 -123.3121 44.56531 0.1 3 159 311626.9 ... 4 1 -123.3119 44.56531 0.1 4 159 311626.9 5 1 -123.3119 44.56485 0.1 5 159 311626.9 6 1 -123.3119 44.56430 0.1 6 159 311626.9 > nrow(ward_sales) [1] 4189 ● Ine ffi cient for complicated spatial objects

  4. Working with Geospatial Data in R Data frames aren’t a great way to store spatial data > head(ward_sales) ward lon lat group order num_sales avg_price 1 1 -123.3128 44.56531 0.1 1 159 311626.9 2 1 -123.3122 44.56531 0.1 2 159 311626.9 3 1 -123.3121 44.56531 0.1 3 159 311626.9 ... 4 1 -123.3119 44.56531 0.1 4 159 311626.9 5 1 -123.3119 44.56485 0.1 5 159 311626.9 6 1 -123.3119 44.56430 0.1 6 159 311626.9 > nrow(ward_sales) [1] 4189 ● Hierarchical structure gets forced into a flat structure

  5. Working with Geospatial Data in R The sp package: ● provides classes for storing di ff erent types of spatial data ● provides methods for spatial objects, for manipulation ● is useful for point, line and polygon data ● is a standard, so new spatial packages expect data in an sp object

  6. Working with Geospatial Data in R Let’s practice!

  7. Working with Geospatial Data in R sp and S4

  8. Working with Geospatial Data in R Two types of sp object > summary(countries_sp) > summary(countries_spdf) Object of class SpatialPolygons Object of class SpatialPolygonsDataFrame Coordinates: Coordinates: min max min max x -180 180.00000 x -180 180.00000 y -90 83.64513 y -90 83.64513 Is projected: FALSE Is projected: FALSE proj4string : proj4string : [+proj=longlat +datum=WGS84 [+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0] +towgs84=0,0,0] Data attributes: name iso_a3 Length:177 Length:177 ... Class :character Class :character Mode :character Mode :character

  9. Working with Geospatial Data in R Two types of sp object > summary(countries_sp) > summary(countries_spdf) Object of class SpatialPolygons Object of class SpatialPolygonsDataFrame Coordinates: Coordinates: min max min max x -180 180.00000 x -180 180.00000 y -90 83.64513 y -90 83.64513 Is projected: FALSE Is projected: FALSE proj4string : proj4string : [+proj=longlat +datum=WGS84 [+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0] +towgs84=0,0,0] Data attributes: name iso_a3 Length:177 Length:177 ... Class :character Class :character Mode :character Mode :character

  10. Working with Geospatial Data in R SpatialPolygons object > str(countries_sp, max.level = 2) Formal class 'SpatialPolygons' [package "sp"] with 4 slots ..@ polygons :List of 177 .. .. [list output truncated] ..@ plotOrder : int [1:177] 7 136 28 169 31 23 9 66 84 5 ... ..@ bbox : num [1:2, 1:2] -180 -90 180 83.6 .. ..- attr(*, "dimnames")=List of 2 ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

  11. Working with Geospatial Data in R SpatialPolygonsDataframe object > str(countries_spdf, max.level = 2) Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 177 obs. of 6 variables: ..@ polygons :List of 177 .. .. [list output truncated] ..@ plotOrder : int [1:177] 7 136 28 169 31 23 9 66 84 5 ... ..@ bbox : num [1:2, 1:2] -180 -90 180 83.6 .. ..- attr(*, "dimnames")=List of 2 ? ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

  12. Working with Geospatial Data in R S4 ● One of R’s object oriented (OO) systems ● Key OO concepts ● class : defines a type of object, their a � ributes and their relationship to other classes. ● methods : functions, behavior depends on class of input ● S4 objects can have a recursive structure, elements are called slots ● h � p://adv-r.had.co.nz/OO-essentials.html#s4

  13. Working with Geospatial Data in R Accessing slots > # 1. Use a dedicated method > proj4string(countries_sp) [1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" > # 2. Use the @ followed by unquoted slot name > countries_sp@proj4string CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 > # 3. Use slot() with quoted slot name > slot(countries_sp, "proj4string") CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0

  14. Working with Geospatial Data in R Let’s practice!

  15. Working with Geospatial Data in R More sp classes and methods

  16. Working with Geospatial Data in R Hierarchy of SpatialPolygonsDataFrame countries_spdf Polygons Polygon @data @Polygons … … @polygons @plotOrder Polygon Polygons @labpt @bbox … … … … Polygon Polygons SpatialPolygonsDataFrame list Polygons list @labpt [,1] [,2] [1,] -94.81758 49.38905 … [2,] -94.64000 48.84000 [3,] -94.32914 48.67074 @coords [4,] -93.63087 48.60926 [5,] -92.61000 48.45000 Polygon

  17. Working with Geospatial Data in R Other sp classes Polygon Polygons SpatialPolygons SpatialPolygonsDataFrame + data.frame @bbox @proj4string

  18. Working with Geospatial Data in R Other sp classes Line Lines SpatialLines SpatialLinesDataFrame + data.frame @bbox @proj4string

  19. Working with Geospatial Data in R Other sp classes SpatialPoints SpatialPointsDataFrame [,1] [,2] + data.frame [1,] -94.81758 49.38905 [2,] -94.64000 48.84000 [3,] -94.32914 48.67074 @bbox [4,] -93.63087 48.60926 [5,] -92.61000 48.45000 @proj4string

  20. Working with Geospatial Data in R Subse � ing sp objects > # Subset by index > str(countries_spdf[1, ], max.level = 2) Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 1 obs. of 6 variables: ..@ polygons :List of 1 ..@ plotOrder : int 1 ..@ bbox : num [1:2, 1:2] 60.5 29.3 75.2 38.5 .. ..- attr(*, "dimnames")=List of 2 ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

  21. Working with Geospatial Data in R Let’s practice!

  22. Working with Geospatial Data in R Introduction to tmap

  23. Working with Geospatial Data in R tmap displays spatial data ● Similar philosophy to ggplot2 : ● a plot is built up in layers ● ggplot2 expects data in data frames, 
 tmap expects data in spatial objects ● layers consist of a type of graphical representation and mapping from visual properties to variables

  24. Working with Geospatial Data in R Building plot in layers > library(tmap) A SpatialPolygonsDataFrame > data(Europe) Specify spatial data > tm_shape(Europe) + tm_borders() Add a layer to the plot

  25. Working with Geospatial Data in R Building plot in layers > library(tmap) > data(Europe) > tm_shape(Europe) + tm_borders() + tm_fill(col = "part") + tm_compass() + tmap_style("cobalt")

  26. Working with Geospatial Data in R Building plot in layers > library(tmap) Adding another data layer > data(Europe) > tm_shape(Europe) + tm_fill() tm_borders() + tm_borders() tm_fill(col = "part") + tm_polygons() tm_compass() + tm_bubbles() tmap_style("cobalt") tm_dots() tm_lines() tm_raster() tm_text()

  27. Working with Geospatial Data in R Building plot in layers > library(tmap) > data(Europe) > tm_shape(Europe) + tm_borders() + tm_fill(col = "part") + tm_compass() + A non-data layer tmap_style("cobalt") Control over other visual elements

  28. Working with Geospatial Data in R Key di ff erences to ggplot2 ● No scale_ equivalents, tweaks to scales happen in relevant layer call ● tm_shape() defines default data for any subsequent layers, you can have many in a single plot ● No need for x and y aesthetics, these are inherent in spatial objects ● No special evaluation, when mapping variables they must be quoted

  29. Working with Geospatial Data in R Let’s practice!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend