knitr::opts_chunk$set( echo = TRUE, tidy.opts = list(width.cutoff = 65), tidy = TRUE) set.seed(12314159) imageDirectory <- "/Users/rwoldford/Documents/Research/talks/MontrealRtutorial2018/Slides/img" dataDirectory <- "/Users/rwoldford/Documents/Research/talks/MontrealRtutorial2018/Slides/data" path_concat <- function(path1, path2, sep="/") {paste(path1, path2, sep = sep)} ## devtools::install_github("rwoldford/loon", subdir="R") ## install.packages(c("maps", "sp", "rworldmap", "RColorBrewer", "scales", ## "ElemStatLearn", "zenplots", "RnavGraphImageData", ## "gridExtra", "dimRed", "vegan", "MASS" ) ) ## ## source("https://bioconductor.org/biocLite.R") ## biocLite(c('graph', 'RDRToolbox', 'Rgraphviz'), ## suppressUpdates=TRUE, suppressAutoUpdate=TRUE) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics0.png")) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics1.png")) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics3.png")) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics4.png")) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics5.png")) knitr::include_graphics(path_concat(imageDirectory, "Rgraphics6.png")) knitr::include_graphics(path_concat(imageDirectory, "loon_fable.png")) library(loon) l_plot(x = quakes$long, y = quakes$lat, xlabel = "longitude", ylabel = "latitude", title = "Tonga trench earthquakes") knitr::include_graphics(path_concat(imageDirectory, "inspectorQuakesOriginal")) knitr::include_graphics(path_concat(imageDirectory, "quakesOriginal")) knitr::include_graphics(path_concat(imageDirectory, "panning.png")) knitr::include_graphics(path_concat(imageDirectory, "zooming.png")) knitr::include_graphics(path_concat(imageDirectory, "selecting.png")) knitr::include_graphics(path_concat(imageDirectory, "selecting.png")) knitr::include_graphics(path_concat(imageDirectory, "inspectorQuakesOriginal")) knitr::include_graphics(path_concat(imageDirectory, "quakesGuides")) knitr::include_graphics(path_concat(imageDirectory, "inspectorPlotAnalysis.png")) knitr::include_graphics(path_concat(imageDirectory, "quakesWorldViewDefault.png")) knitr::include_graphics(path_concat(imageDirectory, "analysisPlotOptions.png")) knitr::include_graphics(path_concat(imageDirectory, "analysisPlotSelection.png")) knitr::include_graphics(path_concat(imageDirectory, "analysisPlotModify.png")) knitr::include_graphics(path_concat(imageDirectory, "inspectorSelectDefaults.png")) knitr::include_graphics(path_concat(imageDirectory, "inspectorQuakesPlus10Cols.png")) l_getColorList() palette() ## l_setColorList_baseR() # base R palette ## l_setColorList_ColorBrewer("Set2") # colorblind friendly choice from ColorBrewer ## l_setColorList_hcl(luminance = 80) # set of hcl colours ## l_setColorList_ggplot2() # ggplot2's palette ## l_setColorList_loon() # default loon palette ## l_setColorList(l_colRemoveAlpha(rainbow(5))) # any set of colours without alpha knitr::include_graphics(path_concat(imageDirectory, "inspectorRbaseCols.png")) knitr::include_graphics(path_concat(imageDirectory, "inspectorColorBrewerCols")) knitr::include_graphics(path_concat(imageDirectory, "inspectorHclCols")) knitr::include_graphics(path_concat(imageDirectory, "inspectorGgplot2Cols")) ## # loon graphics (note that the result is assigned to p) ## p <- l_plot(x = quakes$long, y = quakes$lat, ## xlabel = "longitude", ylabel = "latitude", ## title = "Tonga trench earthquakes") # accessing the plot from its string representation p <- l_create_handle(".l0.plot") p ## library(grid) ## grid.newpage() ## grid.loon(p) library(grid) gp <- grid.loon(p, draw = FALSE) # produces a grob grid.ls(gp) # lists the contents of the grob names(p) p["showGuides"] getGroups <- function(loonplot){ if (!"l_plot" %in% class(loonplot)) stop("loonplot must be an l_plot") lapply(unique(loonplot['color']), FUN = function(group){ loonplot['color'] == group } ) } myGroups <- getGroups(p) # returns groups identified by unique colour in p nGroups <- length(myGroups) # number of groups head(quakes[myGroups[[1]],]) # Data on first few quakes in group 1. p["showGuides"] <- TRUE p["size"] <- sample(1:30, size = length(p["x"]), replace = TRUE) for (i in 1:length(myGroups)) { p["selected"] <- myGroups[[i]] Sys.sleep(1) } p["selected"] <- FALSE # something a little more involved for up to 6 groups myCols <- c("firebrick", "steelblue", "purple", "orange", "grey10", "grey80") for (i in 1:length(myGroups)) { p["color"][myGroups[[i]]] <- myCols[i] } # something crazy for (j in 1:10) { p["xTemp"] <- p["x"] + runif(length(p["x"]), min = -0.5, max = 0.5) Sys.sleep(0.1) } # putting locations and size back p["xTemp"] <- p["x"] p["size"] <- 4 ## library(gridExtra) ## gs <- lapply(myGroups, ## FUN = function(group) { ## p["selected"] <- group ## loonGrob(p) ## }) ## grid.arrange(grobs = gs, ncol = 2) ## p["selected"] <- FALSE library(maps) NZFijiMap <- map("world2", regions=c("New Zealand", "Fiji"), plot=FALSE) l_layer(p, NZFijiMap, label = "New Zealand and Fiji", color = "forestgreen", index = "end") knitr::include_graphics(path_concat(imageDirectory, "quakesGuidesMapInspector")) knitr::include_graphics(path_concat(imageDirectory, "quakesGuidesMap")) knitr::include_graphics(path_concat(imageDirectory, "quakesWorldViewScaling.png")) knitr::include_graphics(path_concat(imageDirectory, "quakesLayersInspector.png")) ## # Base graphics ## plot(x = quakes$long, y = quakes$lat, ## xlab = "longitude", ylab = "latitude", ## main = "Tonga trench earthquakes") ## ## # loon graphics ## l_plot(x = quakes$long, y = quakes$lat, ## xlabel = "longitude", ylabel = "latitude", ## title = "Tonga trench earthquakes") h <- l_hist(quakes$depth, xlabel = "depth", title = "Tonga trench earthquakes") library(RColorBrewer) cols <- brewer.pal(9, "Blues")[-c(1:4)] # get rid of lightest l_setColorList(cols) h["color"] <- cols[cut(quakes$depth, breaks=5)] h_mag <- l_hist(quakes$mag, linkingGroup = "quakes", showStackedColors = TRUE, yshows = "density", xlabel = "magnitude") h_stations <- l_hist(quakes$stations, linkingGroup = "quakes", showStackedColors = TRUE, yshows = "density", xlabel = "Number of stations reporting") p_mag_stations <- l_plot(quakes$mag, quakes$stations, linkingGroup = "quakes", xlabel = "Magnitude", ylabel = "Number of stations reporting")