Creating Interactive Visualisation Tools With R

Overview


  • Themes
    • Interactive Data Analysis
    • Exploratory Spatial Data Analysis
    • Time Series
    • Maps with Backdrops

Initial load up of some libraries and data…


# dplyr library - as in last lecture
library(tidyverse)  
# handle spatial data
library(sp)
# 'nice' color palettes
library(RColorBrewer)
# The rainfall data - as in last lecture
load("rainfall.RData")

More dplyr


rain %>%  group_by(Year,Month) %>% 
  summarise(Rainfall=sum(Rainfall)) %>% ungroup() %>% transmute(Rainfall) %>% 
  ts(start=c(1850,1),freq=12) -> rain_ts
rain_ts %>% window(c(1870,1),c(1871,12))
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1870 2666.2 1975.3 1500.5 1024.8 1862.8  789.2 1038.6 1510.5 2045.5 5177.6
## 1871 3148.3 2343.7 1731.7 2654.5  657.6 2040.1 3705.0 1869.9 2083.4 2774.3
##         Nov    Dec
## 1870 1733.2 1902.2
## 1871 2000.1 1902.0
  • New R commands
    • ungroup - undoes group_by: needed for next command
    • transmute - like mutate but drops unreferenced variables: here used to select out a single column as a variable
    • without the ungroup, transmute would also include Year and Month

A dynamic time series graph


library(dygraphs) # A dynamic graph library - you will need to install it first time
rain_ts %>% dygraph()  # Try moving the pointer along the curve

… with a window


rain_ts %>% window(c(1850,1),c(1889,12)) %>% dygraph(width=800,height=300) 
  • dygraph works in a pipeline
  • If it is at the end, it just produces the dynamic graphic
  • Note also width and height options

An interactive window


rain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector()
  • You can also pipeline dygraphs into functions that add extra controls, like dyRangeSelector.

Also add interactive rolling mean


rain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector() %>% dyRoller(rollPeriod = 600)
  • Another control is dyRoller, a ‘rolling’ mean. Edit number of months in window.
  • Initially use 600 months (50 years)
  • Early values fluctuate more, as less than full window of data is available.

Multiple dygraphs


  • You may need to look at several time series simultaneously
    • and link zooming and windowing operations
    • This can be done with the group option in dygraph
    • For an example, create two time series, for example, Dublin Airport and Belfast
rain %>%  group_by(Year,Month) %>% filter(Station=="Dublin Airport") %>%
    summarise(Rainfall=sum(Rainfall)) %>% ungroup() %>% transmute(Rainfall) %>%
    ts(start=c(1850,1),freq=12) ->  dub_ts
rain %>%  group_by(Year,Month) %>% filter(Station=="Belfast") %>%
    summarise(Rainfall=sum(Rainfall)) %>% ungroup() %>% transmute(Rainfall) %>%
    ts(start=c(1850,1),freq=12) ->  bel_ts
beldub_ts <- cbind(bel_ts,dub_ts)
window(beldub_ts,c(1850,1),c(1850,5))
##          bel_ts dub_ts
## Jan 1850  115.7   75.8
## Feb 1850  120.5   47.8
## Mar 1850   56.8   18.5
## Apr 1850  142.6   97.5
## May 1850   57.9   58.6

The multiple dygraph


beldub_ts %>% dygraph(width=800,height=360) %>% dyRangeSelector

A three-way comparison - with rolling mean


📖 There was no R code for the last example


  • Thats because it is a self-test exercise!
    • Try to recreate this yourself
    • Hints
      1. Filter out the Cork data (Station is 'Cork Airport')
      2. Make the three-way time series with cbind
      3. Create a dygraph then add range selector and roller controls using %>%
    • Answer next week

👀 Sneak preview of an alternative view


To get this to work, wait until next session

dub_ts %>% dygraph(width=800,height=130,group="dub_belf",main="Dublin") 
bel_ts %>% dygraph(width=800,height=170,group="dub_belf",main="Belfast") %>% dyRangeSelector()

👀 Another example


🌍 Interactive Maps

More mapping and the tmap package

library(tmap)
library(sf)
stations_sf <- stations %>% st_as_sf(coords=c('Long','Lat'),crs=4326)
tmap_mode('view')
tm_shape(stations_sf) + tm_bubbles()

Change provider tiles - 1

tm_basemap(leaflet::providers$Esri.OceanBasemap) + 
  tm_shape(stations_sf) + tm_bubbles() 

Change provider tiles - 2

tm_basemap(leaflet::providers$Stamen.Watercolor) + 
  tm_shape(stations_sf) + tm_bubbles() 

Change provider tiles - 3

tm_basemap(leaflet::providers$CartoDB.DarkMatter) + 
  tm_shape(stations_sf) + tm_bubbles() 

Change provider tiles - 4

tm_basemap(leaflet::providers$Stamen.Toner) + 
  tm_shape(stations_sf) + tm_bubbles(col = 'darkred',alpha=0.8) 

Other tile providers

Revisting Polygon Objects

counties <- st_read('counties.geojson')
## Reading layer `counties' from data source `/Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Module/Intro Stats Part/counties.geojson' using driver `GeoJSON'
## Simple feature collection with 40 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 17528.59 ymin: 19537.25 xmax: 366403.6 ymax: 466923.2
## projected CRS:  TM65 / Irish Grid
tm_basemap(leaflet::providers$Stamen.Watercolor) + tm_shape(counties) + tm_borders()

Choropleth mapping

tm_basemap(leaflet::providers$Stamen.Toner) + tm_shape(counties) + tm_polygons(col='mean_rain',alpha=0.5)

Pop-Ups


tm_basemap(leaflet::providers$Stamen.Toner) + tm_shape(counties) +
  tm_polygons(col='mean_rain',alpha=0.5,
              popup.vars='mean_rain',
              popup.format=list(suffix=' cc',prefix=' = '))

Putting it all together


  • Using some ideas from last week
  • Average Rainfall By Station
  • Link to location
  • Colour circles according to rainfall
  • Add them to a map with a legend

1 - Computing the averages


  • left_join links the stations_sf map object to rain_summary to get the geographical information

load('rainfall.RData')
rain %>% group_by(Station) %>%  summarise(mrain=mean(Rainfall))  -> rain_summary
stations_sf %>% left_join(rain_summary) -> station_means_sf
head(station_means_sf, n=4)
## Simple feature collection with 4 features and 8 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -7.37 ymin: 52.28 xmax: -6.77 ymax: 53.6
## geographic CRS: WGS 84
## # A tibble: 4 x 9
##   Station Elevation Easting Northing County Abbreviation Source
##   <chr>       <int>   <dbl>    <dbl> <chr>  <chr>        <chr> 
## 1 Athboy         87  270400   261700 Meath  AB           Met E…
## 2 Foulks…        71  284100   118400 Wexfo… F            Met E…
## 3 Mullin…       112  241780   247765 Westm… M            Met E…
## 4 Portlaw         8  246600   115200 Water… P            Met E…
## # … with 2 more variables: geometry <POINT [°]>, mrain <dbl>

2 - Creating the map

tm_shape(station_means_sf) + tm_bubbles(size='mrain')

Another self-test:


Try re-creating the previous map, but using median rainfall instead of mean. Note the median function can be used in summary.


Conclusion

💡 New ideas


  • New R ideas
    • dygraph
    • popups
    • Next lecture - More Interactive methods