Skip to content

Instantly share code, notes, and snippets.

@wch
Created October 28, 2012 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wch/3969102 to your computer and use it in GitHub Desktop.
Save wch/3969102 to your computer and use it in GitHub Desktop.
Experiments with shiny and ggplot2
library(shiny)
library(datasets)
library(ggplot2)
tg <- ToothGrowth
tg$dose <- factor(tg$dose)
# Define server logic
shinyServer(function(input, output) {
# Versions of ggplot2 < 0.9.3 had a bug with dotplot dodging
if (packageVersion("ggplot2") < as.package_version("0.9.3"))
old_ggplot2 <- TRUE
else
old_ggplot2 <- FALSE
# Tell the webpage that we're using the old or new version
output$old_ggplot2 <- reactive(function() old_ggplot2)
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$tgPlot <- reactivePlot(function() {
# Figure out the aesthetic mappings. color_var requires special handling
# because it can be "none".
if (input$color_var == "") {
aes_mapping <- aes_string(x = input$x_var, y = "len")
} else {
aes_mapping <- aes_string(x = input$x_var, y = "len",
fill = input$color_var)
}
p <- ggplot(tg, mapping = aes_mapping)
if (input$geom_violin) {
p <- p + geom_violin(trim = input$violin_trim,
adjust = input$violin_adjust,
position = position_dodge(input$dodgewidth))
}
if (input$geom_boxplot) {
if (input$boxplot_outliers)
outlier_color <- "black"
else
outlier_color <- NA
p <- p + geom_boxplot(width = input$boxplot_width,
notch = input$boxplot_notch,
outlier.colour = outlier_color,
outlier.size = input$boxplot_outlier_size,
position = position_dodge(input$dodgewidth))
}
if (input$geom_dotplot) {
if (old_ggplot2) {
dotplot_dodge <- "dodge"
} else {
dotplot_dodge <- position_dodge(input$dodgewidth)
}
p <- p + geom_dotplot(binaxis="y", stackdir=input$dotplot_stackdir,
method = input$dotplot_method,
binwidth = input$dotplot_binwidth,
alpha = input$dotplot_alpha,
# Using position=position_dodge() is broken in ggplot2 0.9.2.1.
# So just use "dodge"
position = dotplot_dodge)
}
if (input$geom_point) {
p <- p + geom_point(shape = 21, size = input$point_size,
colour = "black",
alpha = input$point_alpha,
position = position_dodge(input$dodgewidth))
}
print(p)
})
# Show the first "n" observations
output$data_view <- reactiveTable(function() {
head(ToothGrowth, n = input$show_nrows)
})
})
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Visualizing ToothGrowth data set with different geoms"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
selectInput("x_var", "x variable:",
c("Dose" = "dose",
"Supplement Type" = "supp")),
selectInput("color_var", "fill color variable:",
c("None" = "",
"Dose" = "dose",
"Supplement Type" = "supp"),
selected = "Supplement Type"),
conditionalPanel(
condition = "input.color_var != ''",
sliderInput("dodgewidth", "Horizontal dodging width (for different colors):",
min = 0.1, max = 1, value = .9, step = 0.1),
conditionalPanel(
condition = "output.old_ggplot2",
p("The server is using a version of ggplot2 prior to 0.9.3. Note that
this version has a bug where it doesn't allow control over dodging
width of dotplots.")
)
),
wellPanel(
checkboxInput("geom_point", strong("Geom: point"), FALSE),
conditionalPanel(
condition = "input.geom_point",
sliderInput("point_alpha", "Alpha (transparency):",
min = 0.0, max = 1, value = 1, step = 0.1),
sliderInput("point_size", "Size:",
min = 0.5, max = 8, value = 3, step = 0.5)
)
),
wellPanel(
checkboxInput("geom_dotplot", strong("Geom: dotplot"), TRUE),
conditionalPanel(
condition = "input.geom_dotplot",
radioButtons("dotplot_method", "Binning method",
c("Dot-density" = "dotdensity",
"Histodot (regular spacing)" = "histodot")),
sliderInput("dotplot_binwidth", "Bin width:",
min = 0.5, max = 3, value = 1, step = 0.25),
sliderInput("dotplot_alpha", "Alpha (transparency):",
min = 0.0, max = 1, value = 1, step = 0.1),
selectInput("dotplot_stackdir", "Stacking direction:",
c("Centered" = "center",
"Centered-aligned" = "centerwhole"))
)
),
wellPanel(
checkboxInput("geom_boxplot", strong("Geom: boxplot"), FALSE),
conditionalPanel(
condition = "input.geom_boxplot",
sliderInput("boxplot_width", "Width:",
min = 0.1, max = 1, value = .5, step = 0.1),
checkboxInput("boxplot_notch", "Notched", FALSE),
checkboxInput("boxplot_outliers", "Show outliers", TRUE),
conditionalPanel(
condition = "input.boxplot_outliers == true",
sliderInput("boxplot_outlier_size", "Outlier size:",
min = 1, max = 8, value = 3, step = 1)
)
)
),
wellPanel(
checkboxInput("geom_violin", strong("Geom: violin"), FALSE),
conditionalPanel(
condition = "input.geom_violin",
sliderInput("violin_adjust", "Bandwidth adjustment ratio:",
min = 0.25, max = 2, value = 1, step = 0.25),
checkboxInput("violin_trim", "Trim violins to data range", TRUE)
)
)
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
plotOutput("tgPlot"),
sliderInput("show_nrows", "Show N rows of data in table below:",
min = 1, max = 60, value = 10, step = 5),
tableOutput("data_view")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment