knitr::opts_chunk$set( echo = TRUE, tidy.opts = list(width.cutoff = 65), tidy = TRUE) set.seed(12314159) library(loon) data(minority) 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)} # Get some data library("ElemStatLearn") data("SAheart") # Pipes and data manipulation functions library(magrittr) library(tidyverse) SAHealthy <- SAheart %>% filter(famhist == "Absent", chd == 0) %>% arrange(sbp) SAHealthy %>% select(age, ldl) %>% l_plot(linkingGroup ="Healthy heart data") -> p SAHealthy %>% select(age, ldl) %>% l_plot(showGuides = TRUE, linkingGroup ="Healthy heart data") %T>% l_layer_line(x=c(2,100), y=c(0,12), color="red", dash=c(20,10), linewidth = 2) %T>% l_scaleto_world() -> p SAheart %>% mutate(ltob = log(tobacco), lsbp = log(sbp)) %>% filter(age < 50) %>% ggplot(aes(x = ltob, y = lsbp)) + geom_point() + facet_wrap(~chd) -> p ## library(loon.ggplot) ## ## SAheart %>% ## mutate(ltob = log(tobacco), lsbp = log(sbp)) %>% ## filter(age < 50) %>% ## ggplot(aes(x = ltob, y = lsbp)) + ## geom_point() + ## facet_wrap(~chd) %>% ## loon.ggplot() ## ## library(loon.ggplot) ## p <- ggplot(quakes, ## aes(x = long, y = lat, ## colour = factor(cut(quakes$depth, 100, label = FALSE)))) + ## geom_point(size = 4) + ## labs(x = "Longitude", y = "latitude") + ## scale_colour_hue() + ## theme(legend.position="none") + ## facet_grid(~Depth) ## ## loon.ggplot(p, linkingGroup = "quakes") ## # ## library(loon.ggplot) ## library(lattice) ## data(Oats, package = "MEMSS") ## tp1.oats <- xyplot(yield ~ nitro | Variety + Block, data = Oats, type = "o") ## tp1.oats ## ## # The same in ggplot2 ## library(ggplot2) ## pg.oats <- ggplot(Oats, aes(nitro, yield)) + ## geom_line() + ## geom_point() + ## facet_wrap(~Block + Variety, ncol = 3) ## pg.oats ## ## # from ggplot to loon ## loon.ggplot(pg.oats) library(loon) demo(package = "loon") # will produce ## slider_window <- tktoplevel() ## tktitle(slider_window) <- "slider" ## slider_val <- tclVar('1') # default value ## slider <- tkscale(parent = slider_window, ## orient = "horizontal", ## variable = slider_val, ## from = -5, ## to = 5, ## resolution = 0.1, ## command = function(...) print(paste0("Slider value = ", ...))) ## (tkgrid(slider, row = 0, column = 0, sticky="we", padx = 50)) ## tkgrid.columnconfigure(slider_window, 0, weight=1) ## myPlot <- l_plot(iris, color = "Species") ## ## makeItHappen <- function() {print ("It happened!")} ## ## l_bind_state(myPlot, c("color", "xTemp", "yTemp"), makeItHappen) ## ## WhoIsSpecial <- function() { ## cat("Selected points were: \n\t", ## myPlot["itemLabel"][myPlot["selected"]], "\n" ## ) ## } ## ## l_bind_state(myPlot, c("selected"), WhoIsSpecial) ## x <- 1:100 ## y <- 2 + 5 *x ## data <- data.frame(x = x, y = y) ## p <- l_plot(data$x, data$y) ## ## lm_fit <- lm( y ~ x, data = data) ## x_line <- extendrange(x) ## y_line <- predict(lm_fit, newdata = data.frame(x = x_line)) ## fitted_line <- l_layer_line(p, x = x_line, y = y_line, color = "red") ## ## updateLine <- function(myPlot, ls_line = NULL) { ## ## if (!is.null(ls_line) & ls_line %in% l_layer_ids(myPlot)) { ## # we'll update it. ## xnew <- myPlot['xTemp'] ## if (length(xnew) == 0) {xnew <- myPlot['x']} ## ## # For y ## ynew <- myPlot['yTemp'] ## if (length(ynew) == 0) {ynew <- myPlot['y']} ## ## # New fit ## new_fit <- lm( y ~ x, data = data.frame(x = xnew, y = ynew)) ## x_line <- extendrange(xnew) ## y_line <- predict(new_fit, newdata = data.frame(x = x_line)) ## ## # configure the least-squares line ## l_configure(ls_line, x = x_line, y = y_line) ## } ## ## # Update the tcl language's event handler ## tcl('update', 'idletasks') ## } ## l_bind_state(p, ## c("xTemp", "yTemp"), ## function() {updateLine(p, fitted_line)}) knitr::include_graphics(path_concat(imageDirectory, "ThatsAllFolks.png")) knitr::include_graphics(path_concat(imageDirectory, "loon_fable.png"))