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)
# data saved from last week
load('rain_saved.RData')
# 'nice' color palettes
library(RColorBrewer)
# plotting library
library(ggplot2)
library(plotly)
# The rainfall data - as in last lecture
load("rainfall.RData")
Work directly with plotly
Up until now we have created plotly graphs by making ggplot
graphs
Then using ggplotly
But you can create plotly graphics directly
First create some data with a column for Belfast and another for Phoenix Park rainfall
library(tidyverse)
rain_bp %>% spread(key=Station,value=Rainfall) %>% group_by(Year,Month) %>%
summarise(Belfast=max(Belfast,na.rm=TRUE),
Phoenix_Park=max(`Phoenix Park`,na.rm=TRUE),
Date=Date[1]) %>% ungroup() %>% arrange(Date)-> bp
head(bp)
## # A tibble: 6 x 5
## Year Month Belfast Phoenix_Park Date
## <dbl> <fct> <dbl> <dbl> <date>
## 1 1850 Jan 116. 75.7 1850-01-15
## 2 1850 Feb 120. 51.6 1850-02-15
## 3 1850 Mar 56.8 23.2 1850-03-15
## 4 1850 Apr 143. 105. 1850-04-15
## 5 1850 May 57.9 49.6 1850-05-15
## 6 1850 Jun 62 40.5 1850-06-15
A basic example - rainfall in Phoenix Park
plot_ly(bp, x = ~Date, y = ~Phoenix_Park, type = 'scatter', mode = 'lines+markers') -> p
p
Storing plotly
objects allows modification
p %>% layout(title="Phoenix Park Rainfall",
yaxis=list(title="Monthly Total (mm)", showline=TRUE,tickangle=-90)) -> p2
p2
Alternative approach
Start with an empty canvas and add several plot items to it
Plot items are called ‘traces’ in plotly
The canvas has a data set added to it.
Here we plot the empty canvas just to see it, but usually this is pointless
plot_ly(data=bp) -> base
base
Add two time series lines (‘traces’)
base %>%
add_trace(x=~Date,y=~Belfast, type = 'scatter', mode = 'lines+markers', name='Belfast') %>%
add_trace(x=~Date,y=~Phoenix_Park, type = 'scatter', mode = 'lines+markers', name='Phoenix Park') %>%
layout(title='Rainfall Comparison',yaxis=list(title='Monthly Total (mm)',tickangle=-90,showline=TRUE)) -> compare
compare
Using tidyverse
operators on plotly
graphs
It is possibly to use operators like filter
on plotly
base canvases
base %>% filter(Year >= 1980) %>%
add_trace(x=~Date,y=~Belfast, type = 'scatter', mode = 'lines+markers', name='Belfast') %>%
add_trace(x=~Date,y=~Phoenix_Park, type = 'scatter', mode = 'lines+markers', name='Phoenix Park') %>%
layout(title='Rainfall Comparison',
yaxis=list(title='Monthly Total (mm)',tickangle=-90,showline=TRUE)) -> compare2
compare2
A further example with group_by
and summarise
base %>% group_by(Month) %>% summarise(Belfast=mean(Belfast),Phoenix_Park=mean(Phoenix_Park)) %>% ungroup() %>%
add_trace(x=~Month,y=~Belfast, type = 'scatter', mode = 'lines+markers', name='Belfast') %>%
add_trace(x=~Month,y=~Phoenix_Park, type = 'scatter', mode = 'lines+markers', name='Phoenix Park') %>%
layout(title='Rainfall Comparison',
yaxis=list(title='Monthly Average (mm)',tickangle=-90,showline=TRUE)) -> compare3
compare3
Working with subplots
base %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Phoenix_Park,
type = 'scatter', mode = 'lines+markers', name = 'Phoenix Pk') -> phoenix_sp
base %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Belfast, type = 'scatter', mode = 'lines+markers', name = 'Belfast') -> belfast_sp
subplot(phoenix_sp,belfast_sp,nrows = 2) %>%
layout(title="Rainfall Comparison",
yaxis=list(title="Monthly Total (mm)")) -> sub
sub
Working with subplots
base %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Phoenix_Park,
type = 'scatter', mode = 'lines+markers', name = 'Phoenix Pk') -> phoenix_sp
base %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Belfast, type = 'scatter', mode = 'lines+markers', name = 'Belfast') -> belfast_sp
subplot(phoenix_sp,belfast_sp,nrows = 2, shareX=TRUE) %>%
layout(title="Rainfall Comparison",
yaxis=list(title="Monthly Total (mm)")) -> sub
sub
Reintroducing highlight keys with subplots
bp %>% filter(Year >= 1980) %>% highlight_key(~Year) -> bp2
plot_ly(data=bp2) -> base2
base2 %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Phoenix_Park,
type = 'scatter', mode = 'lines+markers', name = 'Phoenix Pk') -> phoenix_sp
base2 %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Belfast, type = 'scatter', mode = 'lines+markers', name = 'Belfast') -> belfast_sp
subplot(phoenix_sp,belfast_sp,nrows = 2, shareX=TRUE) %>%
layout(title="Rainfall Comparison",
yaxis=list(title="Monthly Total (mm)")) -> sub2
sub2
Reintroducing highlight keys with subplots
bp %>% filter(Year >= 1980) %>% highlight_key(~Year) -> bp2
plot_ly(data=bp2) -> base2
base2 %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Phoenix_Park,
type = 'scatter', mode = 'lines+markers', name = 'Phoenix Pk') -> phoenix_sp
base2 %>% filter(Year >= 1980) %>%
add_trace(x = ~Date, y = ~Belfast, type = 'scatter', mode = 'lines+markers', name = 'Belfast') -> belfast_sp
subplot(phoenix_sp,belfast_sp,nrows = 2, shareX=FALSE) %>%
layout(title="Rainfall Comparison",
yaxis=list(title="Monthly Total (mm)")) -> sub3
sub3
A scatter plot
base2 %>%
add_trace(x = ~Belfast, y = ~Phoenix_Park, color=~Month, type = 'scatter', mode = 'markers') %>%
layout(title='Monthly Rainfall (mm)',yaxis=list(title='Phoenix Park'))-> scat
scat
A Final subplot technique - Subplot nesting.
subplot(sub3,scat,nrows = 1)