dygraph
as follows:rain %>% group_by(Year,Month) %>% filter(Station=="Cork Airport") %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1850,1),freq=12) -> cork_ts
bdc_ts <- cbind(bel_ts,dub_ts,cork_ts)
bdc_ts %>% dygraph(width=800,height=450) %>% dyRangeSelector %>% dyRoller
Year
then Month
.load('rainfall.RData')
library(dplyr)
rain %>% arrange(Year,Month) -> rain2
head(rain2,n=4) # Note n=4 reduces the number of observations
## # A tibble: 4 x 4
## Year Month Rainfall Station
## <dbl> <fct> <dbl> <chr>
## 1 1850 Jan 169 Ardara
## 2 1850 Jan 96.9 Derry
## 3 1850 Jan 108. Malin Head
## 4 1850 Jan 92.5 Armagh
monthplot
for a given station
local_monthplot <- function(station,raindata) {
raindata %>% filter(Station == station) -> local_rd
local_rd$Rainfall %>% ts(freq=12,start=1850) -> rain_ts
rain_ts %>% monthplot(col='dodgerblue',col.base='indianred',lwd.base=3)
title(station)
}
monthplot
s in use:local_monthplot('Derry',rain2)
png('test.png',width=400,height=300)
local_monthplot('Derry',rain2)
dev.off()
test.png
library(leaflet)
library(mapview)
library(leafpop)
stations %>% mutate(Filename=file.path(getwd(),paste0(Station,'.png'))) -> files
files %>% select(Station,Filename) %>% head
## # A tibble: 6 x 2
## Station Filename
## <chr> <chr>
## 1 Athboy /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
## 2 Foulksmills /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
## 3 Mullingar /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
## 4 Portlaw /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
## 5 Rathdrum /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
## 6 Strokestown /Users/chrisbrunsdon/Dropbox/climate_change/Time Series etc Modul…
paste0
joins character variables together
paste
with no spacingselect
pulls out variables of interesttmap
is built on another package called leaflet
leaflet
is more powerful and fklexibleleaflet
is generally harder to usetmap
but switch to leaflet
tmap
object in 'view'
modetmap_leaflet
library(sf)
library(tmap)
counties <- st_read('counties.geojson') %>% st_transform(4326) # leaflet objects must be long/lat (epsg 4326)
tmap_mode('view')
tm_start <- tm_shape(counties) + tm_borders()
tm_start
tmap_leaflet(tm_start) %>%
addPolygons(data=counties,
color = NA,
popup = popupImage('test.png',embed = TRUE))
monthplot
for each station
for (i in 1:nrow(files))
with(files, {
png(Filename[i],width=400,height=300)
local_monthplot(Station[i],rain2)
dev.off()} )
png
for each Station
monthplot
a pop-upfiles %>% mutate(Popup=paste0('<img src="',Filename,'">')) -> files
files %>% select(Station,Popup) %>% head
## # A tibble: 6 x 2
## Station Popup
## <chr> <chr>
## 1 Athboy "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
## 2 Foulksmills "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
## 3 Mullingar "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
## 4 Portlaw "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
## 5 Rathdrum "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
## 6 Strokestown "<img src=\"/Users/chrisbrunsdon/Dropbox/climate_change/Time Seri…
"
and '
'
in a string delimited by "
Stations
rain %>% group_by(Station) %>% summarise(mrain=mean(Rainfall)) %>% left_join(files) %>%
select(Station,Long,Lat,mrain,Filename) %>%
st_as_sf(coords=c('Long','Lat'),crs=4326) -> station_means
station_means %>% head
## Simple feature collection with 6 features and 3 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -8.29 ymin: 52.19 xmax: -5.99 ymax: 54.79
## geographic CRS: WGS 84
## # A tibble: 6 x 4
## Station mrain Filename geometry
## <chr> <dbl> <chr> <POINT [°]>
## 1 Ardara 140. /Users/chrisbrunsdon/Dropbox/climate_chan… (-8.29 54.79)
## 2 Armagh 68.3 /Users/chrisbrunsdon/Dropbox/climate_chan… (-6.64 54.35)
## 3 Athboy 74.7 /Users/chrisbrunsdon/Dropbox/climate_chan… (-6.93 53.6)
## 4 Belfast 87.1 /Users/chrisbrunsdon/Dropbox/climate_chan… (-5.99 54.5)
## 5 Birr 70.8 /Users/chrisbrunsdon/Dropbox/climate_chan… (-7.88 53.08)
## 6 Cappoqui… 121. /Users/chrisbrunsdon/Dropbox/climate_chan… (-7.8 52.19)
tmap_leaflet(tm_start) %>%
addCircleMarkers(data=station_means,
popup=popupImage(station_means$Filename,embed=TRUE))
tm_start2 <- tm_shape(station_means) + tm_dots(col='mrain',popup.vars=FALSE,scale=1.5)
tmap_leaflet(tm_start2) %>%
addCircleMarkers(data=station_means,stroke = NA,
popup=popupImage(station_means$Filename,embed=TRUE))
tm_start2
is a new tmap
kick-off map based on shading the dots by mean rainfall.popup_vars = FALSE
stops pop-up on tmap
map
leaflet
and tmap
have pop-upsstroke=NA
stops leaflet
drawing an outer ring on the pop-up markersleaflet
- Scale barstmap_leaflet(tm_start2) %>%
addCircleMarkers(data=station_means,stroke = NA,
popup=popupImage(station_means$Filename,embed=TRUE)) %>%
addScaleBar()
leaflet
- Mini Maptmap_leaflet(tm_start2) %>%
addCircleMarkers(data=station_means,stroke = NA,
popup=popupImage(station_means$Filename,embed=TRUE)) %>%
addMiniMap(zoomLevelOffset = -3)
leaflet
- measuring toolbnds <- st_bbox(counties) # lng1, lat1, lng2, lat2
tmap_leaflet(tm_start2) %>%
addCircleMarkers(data=station_means,stroke = NA,
popup=popupImage(station_means$Filename,embed=TRUE)) %>%
addMeasure()
local_trendplot <- function(station,raindata) {
raindata %>% filter(Station == station) -> local_rd
local_rd$Rainfall %>% ts(freq=12,start=1850) %>%
stl(s.window='periodic',t.window=721) -> rain_stl
rain_ts <- rain_stl$time.series[,1] + rain_stl$time.series[,2]
rain_ts %>% monthplot(col='dodgerblue',col.base='indianred',lwd.base=3)
}
trendplot
s in use:local_trendplot('Derry',rain2)
Create a new pop-up map showing these plots when clicked instead of the original monthplot
s as shown earlier.
tmap
and leaflet