class: center, middle, inverse, title-slide # You Draw It with r2d3 ## ISU Graphics Group ### Emily Robinson, Susan VanderPlas, and Reka Howard ### April 8, 2021 --- class:primary ## Outline + History of fitting lines by eye + You Draw It (NYTimes / with r2d3) + Getting Started with D3.js + r2d3 + R Shiny + Let's see how you do! --- class:primary ## Subjective Judgment in Statistical Analysis: An Experimental Study (1951) [(D. J. Finney; *Journal of the Royal Statistical Society*)](https://rss.onlinelibrary.wiley.com/doi/abs/10.1111/j.2517-6161.1951.tb00093.x) .pull-left[ + **Big Idea:** Determine the effect of stopping iterative maximum likelihood calculations after one iteration. + **Method:** Judge by eye the positions for a pair of *parallel* probit regression lines in a biological assay. Sent out by mail, asked to "rule two lines." + **Sample:** 21 scientists + **Findings:** One cycle of iteration was sufficient. ].pull-right[ <img src="images/subjective-judgement-plot.png" width="75%"/> ] ??? + The subjects for this study were deliberately chosen as. having no' experience of probit methods. --- class:primary ## Scientistis have always had sass! [(Subjective Judgment in Statistical Analysis: An Experimental Study; 1951; D. J. Finney; *Journal of the Royal Statistical Society*)](https://rss.onlinelibrary.wiley.com/doi/abs/10.1111/j.2517-6161.1951.tb00093.x) <font size="3"> .small[ + "No one in their right senses could draw 2 parallel lines through the points in question, but I've done my best to comply with your request as I understand it. **Certainly I did not use my intelligence**". (No. 2.) + "Where fact and theory are so at variance one guess is as good as another-so here goes. The job might have been easier if S.E. of individual points was known!" (No. 9.) + "I should-say that the points were not on two parallel straight lines, and that was that!" (No. 11.) + "The line through the x points is of course easy to draw. But I had to overcome considerable intellectual resistance before I could bring myself to draw a parallel line for the 0 points". (No. 14.) + "**What fun! But I don't believe a word of it.** The only thing is either (a) to do the experiment again or (b) revise the theory". (No. 18.) + **"If, as an experimental scientist, an experiment of mine produced a set of data such as you provided I should at once do the experiment again!"** (No. -20-a chemist.) + "What I would really like to draw are 2 lines which are far from parallel!" (No. 21.) ] </font> --- class:primary ## Eye Fitting Straight Lines (1981) [(Frederick Mosteller, Andrew F. Siegel, Edward Trapido & Cleo Youtz; *The American Statistician*)](https://www.tandfonline.com/doi/abs/10.1080/00031305.1981.10479335) .pull-left[ + **Big Idea:** Students fitted lines by eye to four sets of points. + **Method:** 8.5 x 11 inch transparency with a straight line etched completely across the middle. + **Sample:** 153 graduate students and post docs in Introductory Biostatistics. + **Experimental Design:** Latin square with packets stapled in four different orders. + **Findings:** Students tended to fit the slope of the first principal component. ].pull-right[ <img src="images/eyefitting-straight-lines-plots.png" width="85%"/> ] ??? + Method involves maneuvering a string, black thread, or ruler until the fit seems satisfactory, and then drawing the line. + Sample - most had not studied statistics before + Method proposed by Tukey + Students tended to fit the slope of the first principal component or major axis (the line that minimizes the sum of squares of perpendicular rather than vertical distances). + Students had a slight tendency to choose consistently either steeper or shallower slopes for all sets of data. + Individual-to-individual variability in slope and in intercept was near the standard error provided by least squares for the four data sets (no theory encourages us to believe in such relations). + Found no trend in the differences due to order. --- class:primary ## NYTimes You Draw It .pull-left[ <img src="images/NYTimesYouDrawIt.png" width="100%"/> <font size="2">https://presentyourstory.com/ny-times-you-draw-it-charts/ </font> ].pull-right[ + [Family Income affects college chances](https://www.nytimes.com/interactive/2015/05/28/upshot/you-draw-it-how-family-income-affects-childrens-college-chances.html) + [Just How Bad Is the Drug Overdose Epidemic?](https://www.nytimes.com/interactive/2017/04/14/upshot/drug-overdose-epidemic-you-draw-it.html) + [What Got Better or Worse During Obama’s Presidency](https://www.nytimes.com/interactive/2017/01/15/us/politics/you-draw-obama-legacy.html?_r=0) ] --- class:primary ## You Draw It with `r2d3` .pull-left[ <img src="images/exponential_example.gif" width="100%"/> ].pull-right[ <img src="images/eyefitting_example.gif" width="100%"/> ] <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> See [GitHub repository](https://github.com/srvanderplas/Perception-of-Log-Scales/tree/master/you-draw-it-development/you-draw-it-pilot-app) for shinydrawr-d3v5.js and R Shiny code. --- class:primary ## Getting Started with D3.js `D3.js` is to JavaScript as `ggplot2` is to R .pull-left[ <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M0 32v448h448V32H0zm243.8 349.4c0 43.6-25.6 63.5-62.9 63.5-33.7 0-53.2-17.4-63.2-38.5l34.3-20.7c6.6 11.7 12.6 21.6 27.1 21.6 13.8 0 22.6-5.4 22.6-26.5V237.7h42.1v143.7zm99.6 63.5c-39.1 0-64.4-18.6-76.7-43l34.3-19.8c9 14.7 20.8 25.6 41.5 25.6 17.4 0 28.6-8.7 28.6-20.8 0-14.4-11.4-19.5-30.7-28l-10.5-4.5c-30.4-12.9-50.5-29.2-50.5-63.5 0-31.6 24.1-55.6 61.6-55.6 26.8 0 46 9.3 59.8 33.7L368 290c-7.2-12.9-15-18-27.1-18-12.3 0-20.1 7.8-20.1 18 0 12.6 7.8 17.7 25.9 25.6l10.5 4.5c35.8 15.3 55.9 31 55.9 66.2 0 37.8-29.8 58.6-69.7 58.6z"></path></svg> [Codecademy: Introduction to JavaScript](https://www.codecademy.com/learn/introduction-to-javascript) <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm-6 336H54a6 6 0 0 1-6-6V118a6 6 0 0 1 6-6h404a6 6 0 0 1 6 6v276a6 6 0 0 1-6 6zM128 152c-22.091 0-40 17.909-40 40s17.909 40 40 40 40-17.909 40-40-17.909-40-40-40zM96 352h320v-80l-87.515-87.515c-4.686-4.686-12.284-4.686-16.971 0L192 304l-39.515-39.515c-4.686-4.686-12.284-4.686-16.971 0L96 304v48z"></path></svg> Understand [SVG](http://tutorials.jenkov.com/svg/g-element.html) elements: inspect elements in web browser! <svg viewBox="0 0 576 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M542.22 32.05c-54.8 3.11-163.72 14.43-230.96 55.59-4.64 2.84-7.27 7.89-7.27 13.17v363.87c0 11.55 12.63 18.85 23.28 13.49 69.18-34.82 169.23-44.32 218.7-46.92 16.89-.89 30.02-14.43 30.02-30.66V62.75c.01-17.71-15.35-31.74-33.77-30.7zM264.73 87.64C197.5 46.48 88.58 35.17 33.78 32.05 15.36 31.01 0 45.04 0 62.75V400.6c0 16.24 13.13 29.78 30.02 30.66 49.49 2.6 149.59 12.11 218.77 46.95 10.62 5.35 23.21-1.94 23.21-13.46V100.63c0-5.29-2.62-10.14-7.27-12.99z"></path></svg> Amelia Wattenberger's [Full Stack D3 and Data Visualization Book](https://www.newline.co/fullstack-d3) <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z"></path></svg> Build a basic graphic using [r2d3](https://rstudio.github.io/r2d3/articles/introduction.html) <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm0 448c-110.3 0-200-89.7-200-200S137.7 56 248 56s200 89.7 200 200-89.7 200-200 200zm-80-216c17.7 0 32-14.3 32-32s-14.3-32-32-32-32 14.3-32 32 14.3 32 32 32zm160 0c17.7 0 32-14.3 32-32s-14.3-32-32-32-32 14.3-32 32 14.3 32 32 32zm16 16H152c-26.5 0-48 21.5-48 48v32c0 26.5 21.5 48 48 48h192c26.5 0 48-21.5 48-48v-32c0-26.5-21.5-48-48-48zm-168 96h-24c-8.8 0-16-7.2-16-16v-8h40v24zm0-40h-40v-8c0-8.8 7.2-16 16-16h24v24zm64 40h-48v-24h48v24zm0-40h-48v-24h48v24zm64 40h-48v-24h48v24zm0-40h-48v-24h48v24zm56 24c0 8.8-7.2 16-16 16h-24v-24h40v8zm0-24h-40v-24h24c8.8 0 16 7.2 16 16v8z"></path></svg> Modify `D3.js` code until it does what you want! ] .pull-right[ **Additional Resources** <svg viewBox="0 0 640 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M278.9 511.5l-61-17.7c-6.4-1.8-10-8.5-8.2-14.9L346.2 8.7c1.8-6.4 8.5-10 14.9-8.2l61 17.7c6.4 1.8 10 8.5 8.2 14.9L293.8 503.3c-1.9 6.4-8.5 10.1-14.9 8.2zm-114-112.2l43.5-46.4c4.6-4.9 4.3-12.7-.8-17.2L117 256l90.6-79.7c5.1-4.5 5.5-12.3.8-17.2l-43.5-46.4c-4.5-4.8-12.1-5.1-17-.5L3.8 247.2c-5.1 4.7-5.1 12.8 0 17.5l144.1 135.1c4.9 4.6 12.5 4.4 17-.5zm327.2.6l144.1-135.1c5.1-4.7 5.1-12.8 0-17.5L492.1 112.1c-4.8-4.5-12.4-4.3-17 .5L431.6 159c-4.6 4.9-4.3 12.7.8 17.2L523 256l-90.6 79.7c-5.1 4.5-5.5 12.3-.8 17.2l43.5 46.4c4.5 4.9 12.1 5.1 17 .6z"></path></svg> [How to learn D3 with no coding experience](https://www.heshameissa.com/blog/learn-d3) <svg viewBox="0 0 384 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M369.9 97.9L286 14C277 5 264.8-.1 252.1-.1H48C21.5 0 0 21.5 0 48v416c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V131.9c0-12.7-5.1-25-14.1-34zM332.1 128H256V51.9l76.1 76.1zM48 464V48h160v104c0 13.3 10.7 24 24 24h104v288H48z"></path></svg> [Kiegan's ISU Graphics Group Presentation from Mar 25, 2021](https://kiegan.github.io/talks/graphics-group-r2d3/graphics-group-r2d3.html#1) <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> Amelia Wattenberger on [Twitter](https://twitter.com/Wattenberger) ] --- class:primary ## r2d3 ➕ R Shiny .center[ <img src="images/r2d3+shiny-inkscape.png" width="80%"/> ] ??? To distinguish between code that runs at initialization-time only and code that runs when data changes, organize your code so that the code which responds to data changes is contained within the r2d3.onRender() An explicit resize handler redraws plot as you resize your browser window --- class:primary ## r2d3 ➕ R Shiny ```r data_to_json <- function(data) { jsonlite::toJSON(data, dataframe = "rows", auto_unbox = FALSE, rownames = TRUE) } ``` ```r data <- list(line_data, point_data) # Create a list of simulated point and line data message_loc <- session$ns("drawr_message") # Create storage for data from d3.js r2d3( data = data_to_json(data), # Convert data list to a JSON file script = "shinydrawr-d3v5.js", # Provide the D3.js code file dependencies = c("d3-jetpack"), d3_version= "5", # Specify D3 version options = list(message_loc, ...) # Communciate parameters from R Shiny to D3.js ) ``` --- class:primary ## r2d3 ➕ R Shiny ```r function draw_true_line({svg, line_data, draw_start}, scales){ var df = line_data.filter(function(d){ return d.x <= draw_start}) state.svg.selectAppend("path.shown_line") .datum(df) .at(default_line_attrs) .attr("d", scales.line_drawer); } ``` ```r function draw_points({svg, point_data, points_end, points}, scales){ const dots = state.svg.selectAll("circle").data(df) dots .enter().append("circle") .merge(dots) .attr("cx", d => scales.x(d.x)) .attr("cy", d => scales.y(d.y)) .attr("r", 2) .style("fill", "black") .style("opacity", 0.8) .style("stroke", "black") } ``` --- class:primary ## r2d3 ➕ R Shiny ```r const on_drag = function(){ const drag_x = scales.x.invert(d3.event.x); const drag_y = scales.y.invert(d3.event.y); fill_in_closest_point(state, drag_x, drag_y); draw_user_line(state, scales); draw_rectangle(state, scales); }; ``` ```r const on_end = function(){ const line_status = get_user_line_status(state); if(line_status === 'done'){ # User has completed line drawing ... if(state.shiny_message_loc){ # Make sure shiny is available if(typeof Shiny !== 'undefined'){ Shiny.onInputChange( # Send drawn points off to server state.shiny_message_loc, state.drawable_points.map(d => d.y) ); ... }; ``` --- class:primary ## r2d3 ➕ R Shiny ```r function setup_draw_watcher(svg, scales, on_drag, on_end){ ... .call( d3.drag() .on("drag", on_drag) .on("end", on_end) ); } ``` --- class:primary ## Let's see how you do! + Visit https://shiny.srvanderplas.com/eye-fitting/ to participate. + Share your opinions and feedback as you're drawing! .center[ <img src="images/drawing.gif" width="60%"/> ] ??? https://emily-robinson.shinyapps.io/you-draw-it-pilot-app/ --- class:primary ## References <font size="4"> .small[ + Finney, D. J. (1951). Subjective judgment in statistical analysis: An experimental study. Journal of the Royal Statistical Society: Series B (Methodological), 13(2), 284-297. + Frederick Mosteller , Andrew F. Siegel , Edward Trapido & Cleo Youtz (1981) Eye Fitting Straight Lines, The American Statistician, 35:3, 150-152 + NYTimes you draw it charts. (2017, January 16). Retrieved April 05, 2021, from https://presentyourstory.com/ny-times-you-draw-it-charts/ ] </font>