Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FISH 6002: Week 12 - Interactive plots

FISH 6002: Week 12 - Interactive plots

MI Fisheries Science

November 24, 2017
Tweet

More Decks by MI Fisheries Science

Other Decks in Science

Transcript

  1. Week 12: Interactive plots FISH 6000: Science Communication for Fisheries

    Brett Favaro 2017 This work is licensed under a Creative Commons Attribution 4.0 International License
  2. Why? - Explore complex data - Promote scientific curiosity and

    engagement - Rapidly test hypotheses - Promote experimentation, particularly among general public - Allow for real-time exploration of model output
  3. Examples http://carboncounter.com/ https://github.com/d3/d3/wiki/Gallery https://rviews.rstudio.com/2017 /05/03/shiny-in-medicine/ http://www.tylernwolf.com/ ?utm_source=d3_org&utm_ medium=demo_link&utm_c ampaign=d3_website#/portf olio/corrdisp

    http://riskcalc.org/ ColorectalCancer/ https://canada.energypolicy.solutions/ https://getdolphins.com/blog/interactive-data-visualizations- new-york-times/ https://trase.earth/
  4. ui = User Interface: All the stuff that collects input

    from the user Input types Layout Additional element in layout Output type
  5. renderPlot creates the plot space The rest of the code

    in the {} draws the plot itself Final code to execute the plot
  6. The faithful vector is built-in to R This half sets

    up layout and collects user input… …while this half executes code and produces output (here, a plot)
  7. To read in your own data: Embed the read.csv within

    your server code, and before renderPlot Note the use of relative file paths. Organize Shiny apps using R Projects
  8. Load package Load data Create a subset of the data,

    including only data with X values less than the value grabbed from the user interface (expressed as input$plotrange) Model X by Y from our subset Create a scatterplot with Y as a function of X Draw a straight line based on the linear model (i.e. a “line of best fit”)
  9. Time = 15 min • Open the Minimal Example R

    Project • Change layout, input type, and output. E.g. • Layout: Make it a flow layout, split layout, or vertical layout • Input: Instead of a slider, make it multiple choice, • Output: Advanced - Try another output style Let’s modify the above code
  10. Minimal example: Multiple File File → New File → Shiny

    Web App Divides code into two files ui.R server.R
  11. library(shiny) # Define UI for application that draws a scatterplot

    ui <- fluidPage( # Application title titlePanel("Minimal Example"), # Sidebar with a slider input for x range sidebarLayout( sidebarPanel( sliderInput("plotrange", "Maximum X value:", min = 1, max = 100, value = 50) ), # Create a plot for the XY data mainPanel( plotOutput("xyPlot") ) ) ) # Define server logic required to draw a scatterplot with a simple linear model server <- function(input, output) { library(tidyverse) MinimalExample <- read.csv("../Data/MinimalExample.csv") output$xyPlot <- renderPlot({ datasubset <- MinimalExample %>% filter(X <= input$plotrange) model <- lm(Y~X, data=datasubset) plot(Y~X, data=datasubset) abline(model) }) } # Run the application shinyApp(ui = ui, server = server) app.R ui.R server.R library(shiny) ui <- fluidPage( # Application title titlePanel("Minimal Example"), # Sidebar with a slider input for x range sidebarLayout( sidebarPanel( sliderInput("plotrange", "Maximum X value:", min = 1, max = 100, value = 50) ), # Create a plot for the XY data mainPanel( plotOutput("xyPlot") ) ) ) library(shiny) server <- function(input, output) { library(tidyverse) MinimalExample <- read.csv("../Data/MinimalExample.csv") output$xyPlot <- renderPlot({ datasubset <- MinimalExample %>% filter(X <= input$plotrange) model <- lm(Y~X, data=datasubset) plot(Y~X, data=datasubset) abline(model) }) } One-file: Two-file: Both methods are technically equivalent. Do whatever works for you
  12. Real-world example: the cod rebuilder “Cod are coming back” “Cod

    will recover in 3-5 years” “DFO is lying about how many cod there are” How can we visualize the recovery of cod, so we can have a more informed public debate about the issue?
  13. Goals: • Produce a simple visualization of the recovery of

    cod, given exponential growth at a rate specified by the user: • “I think cod will recover at X% per year”  show what this looks like (a curve) • Calculate how many years it will take for cod to grow beyond ‘critical’ abundance zone, given the user’s assumed growth rate • Allow the user to ‘correct’ for DFO’s “errors” – i.e. if you think there are TWICE as many fish in the sea as DFO says, let them put that into the model • Allow the user to set a custom recovery target (how many cod do we WANT there to be?) • Show this recovery in historical context (how many cod will there be vs, 100 years ago?) • Be transparent with all assumptions (i.e. extensive documentation)
  14. input$ tells R to grab those values from the user

    interface Cod rebuilder code is long – let’s just focus on stuff pertaining to interactivity
  15. Load packages and functions needed for second plot polyCurve function

    courtesy of: https://www.fromthebottomoftheheap.net/2013/01/11/shading-regions-under-a-curve/
  16. Adjustments to any slider causes both plots to re-draw The

    growth function, polygon, height of red line, and X and Y ranges are all re-calculated for each plot
  17. Use of RMarkdown allows for extensive documentation The Cod Rebuilder

    makes tons of assumptions – but it’s transparent about them
  18. Interactive Plots recap • If you are R-literate, you are

    also interactive plots-literate • Any model, plot, etc. can be an interactive plot. If you are inputting data, you can get the user to input data instead – and then you have an interactive plot • This is incredibly powerful. • E.g. Cod rebuilder – don’t take MY word for it, play with the stock YOURSELF. What conditions do you have to meet to rebuild cod in 3-4 years. Do YOU think they’re realistic? Interactive plots allow for such experimentation • E.g. Stock assessment. I used an exponential growth model. There’s zero reason you couldn’t use a proper stock assessment model in this way. • Consider how your own data could be expressed as interactive plots
  19. Course recap •You now have all the basic skills needed

    to work with data in R: •To manipulate it • To plot it • To share it •You can now be reproducible and transparent in your research!
  20. Course recap •We discussed • Collecting data in the field,

    and cleaning data from a database • Data Policy – including data management plans • How to make figures of all kinds, ranging from the most basic to fairly complex, and making them publication- quality • How to save your data and scripts for future reference •You are now ready to do stats in R!