ggvis
packagelibrary(ggvis) load('Rainfall.RData') rain %>% group_by(Year) %>% summarise(mr=mean(Rainfall)) %>% ggvis(x=~Year,y=~mr) %>% layer_lines()
rain %>% group_by(Year) %>% summarise(mr=mean(Rainfall)) %>% ggvis(x=~Year,y=~mr) -> rain_ggv
rain_ggv
rain_ggv %>% layer_lines(stroke := 'firebrick')
:=
for definition
=
selects factorggvis
plots are resizablerain_ggv %>% layer_points(fill := 'navy') %>% layer_lines(stroke := 'firebrick')
ggvis
rain_ggv %>% layer_smooths(span=1,stroke := 'dodgerblue')
span
is the degree of smoothingspan=1
covers whole dataset, but is ‘tapered’rain_ggv %>% layer_smooths(span=1,stroke := 'dodgerblue') %>% layer_points(fill := 'firebrick')
rain_ggv %>% layer_smooths(span=input_slider(0.1,1.5),stroke := 'dodgerblue') %>% layer_points(fill := 'firebrick')
ggvis
can’t do this in a simple presentationrain_ggv %>% layer_points(fill := 'firebrick') %>% layer_smooths(span=waggle(0.1,1.5),stroke := 'dodgerblue')
waggle
should be last item in pipeline
rain_ggv %>% layer_smooths(span=1,stroke := 'dodgerblue') %>% layer_points(fill := 'firebrick') %>% add_axis(type='x',format='4d',subdivide=10) %>% add_axis(type='y',title='Rainfall (cm per year)')
format
works in ggvis
4d
,5d
etc means decimal occupying 4, 5 spaces4,d
etc as above with commas after 1000’s6.2f
display decimals to 2 places03d
pad with leading zeroes - ie 007
# Step 1 - create a January rainfall point data set # Create the January rainfall data ms_rain <- rain %>% group_by(Station,Month) %>% summarise(rain = mean(Rainfall)) ja_rain <- ms_rain %>% filter(Month == 'Jan') # Create an sf point object for the stations, then join the january averages stat_dat <- st_as_sf(stations,coords=c('Long','Lat'),crs=4326) %>% st_transform(29902) %>% left_join(ja_rain)
# First off create the voronoi tesselation (Theissen polys) - Step 2 vor <- stat_dat %>% st_union() %>% st_voronoi() %>% st_collection_extract() %>% st_sf() # Next join back the January rainfall data vor_jan <- vor %>% st_join(stat_dat)
coast <- st_read('ireland_coast.geojson',quiet=TRUE) # Stops printing info vor_jan <- vor_jan %>% st_intersection(coast) tm_shape(vor_jan) + tm_borders() + tm_shape(stat_dat) + tm_dots(size=0.3)
tm_shape(vor_jan) + tm_polygons(col='rain') + tm_legend(legend.outside=TRUE)
library(fields) # You may need to install this package tps_model <- Tps(st_coordinates(stat_dat), stat_dat$rain,method='REML') tps_model
## Call: ## Tps(x = st_coordinates(stat_dat), Y = stat_dat$rain, method = "REML") ## ## Number of Observations: 25 ## Number of parameters in the null space 3 ## Parameters for fixed spatial drift 3 ## Model degrees of freedom: 9.3 ## Residual degrees of freedom: 15.7 ## GCV estimate for sigma: 19.23 ## MLE for sigma: 18.04 ## MLE for rho: 63790 ## lambda 0.0051 ## User supplied rho NA ## User supplied sigma^2 NA ## Summary of estimates: ## lambda trA GCV shat -lnLike Prof converge ## GCV 3.059061e-05 23.750015 209.1276 3.233613 101.4826 NA ## GCV.model NA NA NA NA NA NA ## GCV.one 3.059061e-05 23.750015 209.1276 3.233613 NA NA ## RMSE NA NA NA NA NA NA ## pure error NA NA NA NA NA NA ## REML 5.101444e-03 9.295568 588.6009 19.228773 101.2859 3
sf
sf
object into a rasterlibrary(raster) # You may need to install this rex <- extent(st_bbox(coast)) cover_grid <- raster(rex,resolution=1000) rain_grid <- interpolate(cover_grid,tps_model) crs(rain_grid) <- CRS("+init=epsg:29902") m <- rasterize(coast,rain_grid) rain_grid <- mask(rain_grid,m)
tm_shape(rain_grid) + tm_raster(style='cont',title='Monthly Rainfall (cc)') + tm_shape(coast) + tm_borders()
raster
objects
Thiessen / Voronoi Polygons
Interpolation
Smooth Trend Modelling/Fields
ggviz
and interactive exploration
Next lecture - Reproducible research