# 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")
dplyr
and ggplotlibrary(lubridate)
rain %>% mutate(Date=ymd(paste0(Year,'-',Month,'-15'))) -> rain
rain %>% filter(Station=="Belfast") -> rain_belfast
rain %>% filter(Station=="Phoenix Park") -> rain_phoenix
lubridate
- package to handle dates and timesymd
- 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
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
library(plotly)
plot_phoenix %>% ggplotly()
plot_phoenix %>% ggplotly(tooltip = 'Rainfall', dynamicTicks=TRUE)
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)
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
rain_bp %>% highlight_key(~Date) -> rain_bp_hk
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)
# 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')
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)")
highlight
function specifies the operation of the highlight
persistent
means the highlighted articles stay when you select extra onesselectize
makes the selection control box appeardynamic
and colors
let you change selection colorlayout
allows further changes in the style of the plots# 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 togetherrain %>% 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')
nrow
and ncol
in the help for facet_wrap
save(rain_bp,file='rain_saved.RData')
plotly