dplyrrain %>% 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
ungroup - undoes group_by: needed for next commandtransmute - like mutate but drops unreferenced variables: here used to select out a single column as a variableungroup, transmute would also include Year and Monthlibrary(dygraphs) # A dynamic graph library
rain_ts %>% dygraph # Try moving the pointer along the curve
rain_ts %>% window(c(1850,1),c(1889,12)) %>% dygraph(width=800,height=300)
dygraph works in a pipelinewidth and height optionsrain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector
dyRangeSelector.rain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector %>% dyRoller(rollPeriod = 600)
dyRoller, a ‘rolling’ mean. Edit number of months in window.group option in dygraphrain %>% 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
beldub_ts %>% dygraph(width=800,height=360) %>% dyRangeSelector
'Cork Airport')cbinddygraph then add range selector and roller controls using %>%To get this to work, wait until lecture 4
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
leaflet packagelibrary(leaflet)
leaflet(data=stations,height=430,width=390) %>% addTiles %>%
setView(-8,53.5,6) %>% addCircleMarkers # Works out that 'lat' and 'long' are location
leaflet(data=stations,height=430,width=390) %>% addProviderTiles('CartoDB.Positron') %>%
setView(-8,53.5,6) %>% addCircleMarkers
leaflet(data=stations,height=430,width=390) %>% addProviderTiles('Stamen.Watercolor') %>%
setView(-8,53.5,6) %>% addCircleMarkers
leaflet(data=stations,height=430,width=390) %>% addProviderTiles('CartoDB.DarkMatter') %>%
setView(-8,53.5,6) %>% addCircleMarkers(fillColor='wheat')
leaflet(data=stations,height=430,width=390) %>% addProviderTiles('Stamen.Toner') %>%
setView(-8,53.5,6) %>% addCircleMarkers(fillColor='firebrick',stroke=FALSE,fillOpacity = 0.8)
load("maps.RData")
leaflet(data=counties.spdf,height=430,width=600) %>%
addTiles %>% addPolygons(fillOpacity=0.4,weight=1,color='black') # Note shapefiles must be lat/long projection
color_fun <- colorNumeric(c('wheat','firebrick'),counties.spdf$POPDENSITY)
leaflet(data=counties.spdf,height=430,width=600) %>%
addProviderTiles('CartoDB.Positron') %>% addPolygons(fillOpacity=0.4,weight=1,fillColor=~color_fun(POPDENSITY))
color_fun <- colorQuantile('Greens',counties.spdf$POPDENSITY)
leaflet(data=counties.spdf,height=430,width=600) %>%
addProviderTiles('CartoDB.Positron') %>% addPolygons(fillOpacity=0.4,weight=1,fillColor=~color_fun(POPDENSITY))
leaflet(data=counties.spdf,height=430,width=600) %>% addProviderTiles('CartoDB.Positron') %>%
addPolygons(fillOpacity=0.4,weight=1,fillColor=~color_fun(POPDENSITY),popup=~COUNTYNAME)
leaflet(data=counties.spdf,height=430,width=600) %>% addProviderTiles('CartoDB.Positron') %>%
addPolygons(fillOpacity=0.4,weight=1,fillColor=~color_fun(POPDENSITY)) %>%
addLegend(pal=color_fun,values=~POPDENSITY,title="Percentile of Density",position='bottomleft')
left_join links the stations data frame to rain_summary to get the geographical informationload('rainfall.RData')
rain %>% group_by(Station) %>% summarise(mrain=mean(Rainfall)) -> rain_summary
rain_summary %>% left_join(stations) -> station_means
## Joining by: "Station"
head(station_means, n=4)
## Source: local data frame [4 x 10]
##
## Station mrain Elevation Easting Northing Lat Long County
## (chr) (dbl) (int) (dbl) (dbl) (dbl) (dbl) (chr)
## 1 Ardara 140.36753 15 180787.7 394679.0 54.79 -8.29 Donegal
## 2 Armagh 68.32096 62 287831.3 345772.0 54.35 -6.64 Armagh
## 3 Athboy 74.74356 87 270400.0 261700.0 53.60 -6.93 Meath
## 4 Belfast 87.10995 115 329623.4 363141.3 54.50 -5.99 Antrim
## Variables not shown: Abbreviation (chr), Source (chr)
color_fun <- colorNumeric('Blues',station_means$mrain)
previewColors(color_fun,fivenum(station_means$mrain))
leaflet(data=station_means,height=430,width=600) %>% addProviderTiles('CartoDB.Positron') %>%
setView(-8,53.5,6) %>% addCircleMarkers(fillColor=~color_fun(mrain),weight=0,fillOpacity = 0.85) %>%
addLegend(pal=color_fun,values=~mrain,title="Rainfall",position='bottomleft')
median rainfall instead of mean. Note the median function can be used in summary.
dygraphleafletRColorBrewer