Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats...
Transcript of Spatial Visualisations with R · • Sdfsdf : supports reading from many spatial data formats...
Spatial Visualisations with R
Tatiana [email protected] May 2018
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
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
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
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
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
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
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
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
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
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
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
Adding attributes to spatial data
Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-
registry
LondonR, 8 May 2018
Adding attributes to spatial data
Example: NCP registry data https://data.gov.uk/dataset/national-charge-point-
registry
LondonR, 8 May 2018
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
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
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
Plotting maps with ggmap
LondonR, 8 May 2018
Plotting maps with ggmap
LondonR, 8 May 2018
Plotting maps with ggmap
LondonR, 8 May 2018
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
Interactive plots with leaflet
Interactive plots with leaflet
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
Thank you for listening!
Questions?
LondonR, 8 May 2018