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 librar - as in last lecture
library(dplyr)  
# handle spatial data
library(sp)
# 'nice' color palettes
library(RColorBrewer)
# plotting library
library(ggplot2)
# The rainfall data - as in last lecture
load("rainfall.RData")

More dplyr and ggplot


library(lubridate)
rain %>% mutate(Date=ymd(paste0(Year,'-',Month,'-15')))  -> rain
rain %>% filter(Station=="Belfast") -> rain_belfast
rain %>% filter(Station=="Phoenix Park")  -> rain_phoenix 
  • New R ideas
    • lubridate - package to handle dates and times
    • Calendar dates are a new kind of variable
    • ymd - Turns Month and Year into a calendar date (15th of month)
rain_belfast %>% arrange(Date) %>% head(n=5)
## # A tibble: 5 x 5
##    Year Month Rainfall Station Date      
##   <dbl> <fct>    <dbl> <chr>   <date>    
## 1  1850 Jan      116.  Belfast 1850-01-15
## 2  1850 Feb      120.  Belfast 1850-02-15
## 3  1850 Mar       56.8 Belfast 1850-03-15
## 4  1850 Apr      143.  Belfast 1850-04-15
## 5  1850 May       57.9 Belfast 1850-05-15

A basic time series graph


plot_phoenix <- ggplot(rain_phoenix,aes(x=Date,y=Rainfall)) + geom_line(col='grey50') + geom_point(col='navy',size=0.5)  +labs(title="Phoenix Park")
plot_phoenix

A basic time series graph becomes interactive


library(plotly)
plot_phoenix %>% ggplotly()

A few modifications are possible


plot_phoenix %>% ggplotly(tooltip = 'Rainfall', dynamicTicks=TRUE)

Modify the text on a tooltip


rain_phoenix %>% 
  mutate(`Total Rain`=paste('Rainfall', format(Rainfall,digits = 3),'(mm total)')) ->
  rain_phoenix
plot_phoenix <- ggplot(rain_phoenix,aes(x=Date,y=Rainfall)) + geom_line(col='grey50') +
  geom_point(aes(text=`Total Rain`),col='navy',size=0.5)  +
  labs(title="Phoenix Park",y='Rainfall (mm total)')
plot_phoenix %>% ggplotly(tooltip = 'text', dynamicTicks=TRUE) 

⛓Linking Data Together


Merge the two data sets

rain_belfast %>% 
  mutate(`Total Rain`=paste('Rainfall', format(Rainfall,digits = 3),'(mm total)')) ->
  rain_belfast
rain_belfast %>% bind_rows(rain_phoenix) -> rain_bp
  1. Prepare the Belfast data similarly
rain_bp %>% highlight_key(~Date) -> rain_bp_hk

Linking Views Together 2


plot_bp <- ggplot(rain_bp,aes(x=Date,y=Rainfall,group=Station)) + geom_line(col='grey50') + 
  geom_point(aes(text=`Total Rain`),col='navy',size=0.5)  +
  labs(y='Rainfall (mm total)') + facet_wrap(~Station,nrow=2)
ggplotly(plot_bp,tooltip='text', dynamicTicks=TRUE)

👀 Highlight Keys 1


# Filter out observations post 1980 - then set highlight key to Station
rain %>% filter(Year > 1980) %>% highlight_key(~Station,"Stations") -> rain_hs
# Create a ggplot of date vs. rainfall grouped by Station
ggplot(rain_hs,aes(x=Date,y=Rainfall,group=Station)) + geom_line(col='grey50') + 
 labs(y='Rainfall (mm total)') -> plot_ks
# Turn this into an interactive plotly object
ggplotly(plot_ks,tooltip='Station',dynamicTicks = TRUE) %>%
   highlight(on = "plotly_click", off = "plotly_doubleclick",color='seagreen')

👀 Highlight Keys 2 - more highlight controls


ggplotly(plot_ks,tooltip='Station',dynamicTicks = TRUE) %>%
   highlight(on = "plotly_click", off = "plotly_doubleclick",color=c('seagreen','navy','darkred'),
             selectize = TRUE,persistent = TRUE,dynamic = TRUE) %>% layout(title="Total Rainfall (mm)")

So what do highlight keys do?

  • When you select a variable as a highlight key it ‘tags’ the data set with that information
  • When you click on a point (or other object) it notes the variable
  • All objects with the same value as the highlight key variable are selected
  • the highlight function specifies the operation of the highlight
    • persistent means the highlighted articles stay when you select extra ones
    • selectize makes the selection control box appear
    • dynamicand colors let you change selection color
  • layout allows further changes in the style of the plots

👀 Another example:


# Filter out Birr post 1980 for data.  Highlight key is 'Year'
rain %>% filter(Year > 1980,Station=='Birr') %>% highlight_key(~Year,"Year") -> rain_hy
# Create the ggplot object - monthly rainfalls separated into year groups
ggplot(rain_hy,aes(x=Month,y=Rainfall,group=Year)) + geom_line(col='grey50') + geom_point() +
 labs(y='Rainfall (mm total)') -> plot_ky
# Turn it into a plotly interactive graph
ggplotly(plot_ky,tooltip='Year') %>%
   highlight(on = "plotly_click", off = "plotly_doubleclick",color='navy') %>%
  layout(title="Birr Monthly Rainfall (mm)")

facet_wrap and highlighting are good together


rain %>% filter(Year > 1980,Station %in% c('Birr','Valentia')) %>%
  highlight_key(~Year,"Year") -> rain_hy2
ggplot(rain_hy2,aes(x=Month,y=Rainfall,group=Year)) + geom_line(col='grey50') +
  geom_point(col='seagreen') + facet_wrap(~Station,ncol = 1) + labs(y='mm rain') -> plot_ky2
ggplotly(plot_ky2,tooltip='Year') %>%
   highlight(on = "plotly_click", off = "plotly_doubleclick",color='navy') 

👀 More Stations…


📖 There was no R code for the last example


  • Thats because it is a self-test exercise!
    • Try to recreate this yourself
    • Hints
      1. Add the extra statiins in the data selection
      2. lookup up nrow and ncol in the help for facet_wrap
    • Answer next week

Save a few things for next session:

save(rain_bp,file='rain_saved.RData')

Conclusion

💡 New ideas


  • New R ideas
    • plotly
    • Highlighting and linked selection
    • functions that return functions
    • Next lecture - More Interactive methods