# 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")
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
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 Month
library(dygraphs) # A dynamic graph library - you will need to install it first time
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 dygraph
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
beldub_ts %>% dygraph(width=800,height=360) %>% dyRangeSelector
'Cork Airport'
)cbind
dygraph
then add range selector and roller controls using %>%
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()
tmap
packagelibrary(tmap)
library(sf)
stations_sf <- stations %>% st_as_sf(coords=c('Long','Lat'),crs=4326)
tmap_mode('view')
tm_shape(stations_sf) + tm_bubbles()
tm_basemap(leaflet::providers$Esri.OceanBasemap) +
tm_shape(stations_sf) + tm_bubbles()
tm_basemap(leaflet::providers$Stamen.Watercolor) +
tm_shape(stations_sf) + tm_bubbles()
tm_basemap(leaflet::providers$CartoDB.DarkMatter) +
tm_shape(stations_sf) + tm_bubbles()
tm_basemap(leaflet::providers$Stamen.Toner) +
tm_shape(stations_sf) + tm_bubbles(col = 'darkred',alpha=0.8)
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()
tm_basemap(leaflet::providers$Stamen.Toner) + tm_shape(counties) + tm_polygons(col='mean_rain',alpha=0.5)
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=' = '))
left_join
links the stations_sf
map object to rain_summary
to get the geographical informationload('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>
tm_shape(station_means_sf) + tm_bubbles(size='mrain')
Try re-creating the previous map, but using median
rainfall instead of mean
. Note the median
function can be used in summary
.
dygraph
popups