spatial visualisations with r · • sdfsdf : supports reading from many spatial data formats...

25
Spatial Visualisations with R Tatiana Kim [email protected] 8 May 2018

Upload: others

Post on 06-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Spatial Visualisations with R

Tatiana [email protected] May 2018

Page 2: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Outline

LondonR, 8 May 2018

• Spatial data formats

• Reading spatial data

• Adding attributes to spatial data & aligning projections

• Plotting maps with ggplot2, ggmap and leaflet

Page 3: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Spatial data

A lot of data contains spatial element to it

UK spatial data, including rivers, roads and boundaries

Boundaries of UK administrative and statistical geographies

World map boundaries, including some country data

World map boundaries, including some country data

UK environmental data – Ancient Woodland, National Nature Reserves

LondonR, 8 May 2018

Page 4: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Spatial data types and formats

A lot of public UK data contains spatial element to it

• Shape file format “. shp” which contains all geometric information

“. dbf” which contains the attributes of geometric figures.

“. shx” which enables filtering of geometric data by it’s attributes

• Spatial points data (longitude, latitude)

• Data by region

LondonR, 8 May 2018

Page 5: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Useful libraries to handle spatial data in R

• Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library)

• : provides various mapping functions

• : for data manipulation

• : for data visualisation

• : extends the plotting package ggplot2 for maps

• : for plotting interactive maps

rgdal

maptools

dplyr

ggplot2

ggmap

LondonR, 8 May 2018

leaflet

Page 6: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Reading in spatial data in R

> library(rgdal)

> PolygonsDF <- readOGR(“name-of-file.shp")

PolygonsDF is a Large SpatialPolygonsDataFrame in R, containing

Slot Description

data attributes table

Polygons polygon topology

plotOrder polygon topology

bbox bounding box

proj4string reference coordinate system

LondonR, 8 May 2018

Page 7: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Reading in spatial data in R

> library(rgdal)

> PolygonsDF <- readOGR(“name-of-file.shp")

Boundaries of Middle Super Output Areas in EnglandSource: http://geoportal.statistics.gov.uk/ (7,201)

LondonR, 8 May 2018

Page 8: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Reading in spatial data in R

> library(rgdal)

> PolygonsDF <- readOGR(“name-of-file.shp")

> plot(PolygonsDF)

Boundaries of Middle Super Output Areas in EnglandSource: http://geoportal.statistics.gov.uk/

LondonR, 8 May 2018

Page 9: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Converting Spatial Data into Data Frame

To convert Spatial Polygons Data Frame into Data Frame, use tidy() function in broom package:

DF <- broom::tidy (PolygonsDF)

E02000001

LondonR, 8 May 2018

Page 10: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Adding attributes to spatial data

• Can add points data (long, lat) to the map

Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-

registry

ggplot() + geom_polygon(data = DF,

aes(x=long,y=lat, group = id),

fill = "white", colour = "black")

+ geom_point(data = NCPdata,

aes(x=long, y=lat), color= "red")

LondonR, 8 May 2018

Page 11: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Adding attributes to spatial data

• Can add points data (long, lat) to the map

Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-

registry

LondonR, 8 May 2018

Page 12: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Aligning projections

• contains projections data (reference coordinate system)

• doesn’t!

• Use proj4string() from to get information about the coordinate system

• Use spTransform() from to assign coordinate systems

rgdal

maptools

rgdal

rgdal

LondonR, 8 May 2018

Page 13: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Adding attributes to spatial data

Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-

registry

LondonR, 8 May 2018

Page 14: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Adding attributes to spatial data

Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-

registry

LondonR, 8 May 2018

Page 15: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Adding attributes to spatial data

• Can add additional data that exist for each output area

Example: House prices by

Middle Super Output areas

https://www.ons.gov.uk/peoplepopulationandcommunity/housing/datasets/hpssadataset2medianhousepricebymsoaquarterlyrollingyear

LondonR, 8 May 2018

Page 16: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with ggplot2mergedDF <- merge(DF, HousePrices, by.x = "id", by.y = "MSOA code")

Map <- ggplot() + geom_polygon(data = mergedDF, aes(long, lat, group = id), fill = PriceRange)

LondonR, 8 May 2018

Page 17: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with ggmap

library(ggmap)

map.London <- get_map(location = “London, UK", source = "google")

ggmap(map.London) +

geom_point(data = NCP, aes( x=long, y=lat ), color = "red")

ggmap is a package that uses the ggplot2 syntax as a template to create maps with image tiles taken from map servers such as Google and OpenStreetMap

LondonR, 8 May 2018

Page 18: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with ggmap

LondonR, 8 May 2018

Page 19: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with ggmap

LondonR, 8 May 2018

Page 20: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with ggmap

LondonR, 8 May 2018

Page 21: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Plotting maps with leaflet

library(leaflet)

m <- leaflet(data = NCP) %>%

addTiles() %>% addMarkers(~long, ~lat, label = ~as.character(name))

m

leaflet is an open-source JavaScript library for mobile-friendly interactive maps

LondonR, 8 May 2018

Page 22: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Interactive plots with leaflet

Page 23: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Interactive plots with leaflet

Page 24: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Pros and Cons

Pros: flexible way to visualise spatial data, easy to share the

code, reproducible research

Cons: can be a bit slow processing large amounts of data, need

to manually align different sources of data by region code or by coordinate system

LondonR, 8 May 2018

Page 25: Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats (R’s interface to a popular gdal library) • : provides various mapping functions

Thank you for listening!

Questions?

LondonR, 8 May 2018