Interactive Data Visualization in R
youdrawitR
PackageInteractive Data Visualization in R
Engaging users with data
D3 is not intuitive for us R users
Add additional functionality for broader use
youdrawitR
Install: devtools::install_github("earobinson95/youdrawitR")
Load: library(youdrawitR)
Data generation: customDataGen()
and linearDataGen()
Visualization: drawr()
Package website:https://earobinson95.github.io/youdrawitR/
youdrawitR
customDataGen
and linearDataGen
for data preparation.drawr
More Examples:
Logistic Regression
# Generating data with logistic regression
# For this example, we'll need a binary response variable.
# Let's create a binary variable based on horsepower.
mtcars$high_hp <- ifelse(mtcars$hp > 120, 1, 0)
logistic_data <- customDataGen(
df = mtcars,
xvar = "mpg",
yvar = "high_hp",
regression_type = "logistic"
)
drawr(logistic_data,
title = "Probability High Horsepower vs MPG (Logistic)",
subtitle = "For mtcars dataset",
x_lab = "Miles per Gallon",
y_lab = "High Horsepower (1 = Yes, 0 = No)")
More Examples:
Polynomial Regression & Customized Plot
# Generating data with polynomial regression of degree 2
poly_data <- customDataGen(
df = mtcars,
xvar = "mpg",
yvar = "hp",
regression_type = "polynomial",
degree = 2
)
# Customized drawr output
drawr(poly_data,
title = "Horsepower vs Miles per Gallon (Polynomial)",
subtitle = "For mtcars dataset",
x_lab = "Miles per Gallon",
y_lab = "Horsepower",
drawn_line_color = "red",
true_line_color = "rgba(0,255,0,.8)",
draw_region_color = "#FFFFFF",
show_tooltip = TRUE,
aspect_ratio = 1.5)
More Examples:
Loess Regression
# Generating data with loess regression
loess_data <- customDataGen(
df = mtcars,
xvar = "mpg",
yvar = "hp",
regression_type = "loess",
span = 0.75
)
drawr(loess_data,
title = "Horsepower vs Miles per Gallon (Loess)",
subtitle = "For mtcars dataset",
x_lab = "Miles per Gallon",
y_lab = "Horsepower")
drawr
https://earobinson95.github.io/youdrawitR/articles/customDataGen.html
Install and load youdrawitR
Generate data using the customDataGen
function and your favorite R data set
Pass your generated data into the drawr
function to create your interactive graphic
Using the documentation help(drawr)
make changes to your graphic such as: changing colors, hiding the true line, or adjusting the start of the drawn line (Hint: look at the example in documentation).
youdrawitR
into HTMLyoudrawitR
with ShinyAdd your cat to the slide deck at https://docs.google.com/presentation/d/1prNX34FKP45x9FkRH_f35ejHhDUAdrfRHWAjY_hacQU/edit?usp=sharing
library(shiny)
# Define UI
fluidPage(
# Application title
titlePanel("My drawr plot"),
mainPanel(
# Show the interactive plot of the generated data
r2d3::d3Output("shinydrawr"),
fluidRow(
column(
width = 4,
# Add the reset button
actionButton("resetBtn", "Reset")
),
column(
width = 4,
# Add the checkbox for confidence interval
checkboxInput("confInterval", "Show Confidence Interval", value = TRUE)
),
column(
width = 4,
# Show the user data
tableOutput("user_data")
)
)
)
)
library(shiny)
library(youdrawitR)
function(input, output, session) {
custom_data <- reactive({
customDataGen(
# Paste your previously written code for data generation here!
conf_int = input$confInterval
)
})
observeEvent(input$completedLineData, {
# This block will be executed whenever completedLineData is updated from the JS side
output$user_data <- renderTable({
jsonlite::fromJSON(input$completedLineData)
})
})
observeEvent(input$resetBtn, {
session$sendCustomMessage("resetAction", "true")
})
output$shinydrawr <- r2d3::renderD3({ drawr(custom_data(),
hide_buttons = T,
conf_int = input$confInterval) })
}
youdrawitR
youdrawitR