GOAL: Create a histogram that accepts user input for bin count, and overlay it with a curve to fit the distribution. Plotted data is the amount of time it takes a person to cut a cookie.
KEY FUNCTIONS:
geom_histogram(aes(y = ..count..), bins = input$binCount) - This statement creates a frequency plot with a user-specified number of bins.
geom_density(aes(y = (..density..)(N)(binWidth) )) - This statement is supposed to create a curve that fits the distribution. "N" is the total number of data points (20) and "binWidth" is the width of each bin (default = 5), which varies according the number of bins specified by user. A full explanation of the math behind this transformation can be found here.
PROBLEM: The "aes()" mapping statement within the "geom_density()" function is not recognizing the variables "N" or "binWidth", which are previously created in the "RenderPlot" block.
CODE: The following code is immediately runnable. Line 84 will cause the error. If you'd like to see what the result should look like for David or Sharon (with the default binCount = 5), then you can uncomment Lines 85 or 86.
SIMILAR ISSUES: I've found some posts dealing with similar ggplot issues, such as this post, but they deal mostly with passing strings to the mapping statement using "aes_string()", and I'm using numeric variables.
Thanks for any help you can provide!
#
# Cookie Cutting Analytics
#
# Author: Cody
# Date: 10/16/2017
# Descr: An application to analyze David and Sharon's cookie cutting efficiency.
#
# Libraries -----------------------------------------------------------
suppressWarnings(library(dplyr))
suppressWarnings(library(ggplot2))
suppressWarnings(library(shiny))
# User Interface ------------------------------------------------------
ui <- fluidPage(
# App Title
titlePanel("Cookie Cutting Analytics"),
# Sidebar layout
sidebarLayout(
# Sidebar panel for Input
sidebarPanel(
# Input: Proc Name Dropdown
selectInput("cutterPerson", "Cookie Cutter:",
c("David", "Sharon")),
# Input: Histogram Bin Count Slider
sliderInput("binCount", "Number of Bins:",
min = 1,
max = 10,
value = 5)
),
# Main panel for displaying outputs
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plots",
br(),
plotOutput("histogram"),
br(),
plotOutput("boxPlot")),
tabPanel("Stats", verbatimTextOutput("summary")),
tabPanel("Data", tableOutput("table"))
)
)
)
)
# Server Logic --------------------------------------------------------
server <- function(input, output) {
# Reactive Expression: Cookie Data
cookieData.df <- reactive ({
person <- c("David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon","David","Sharon")
cutTime <- c(5,10,8,12,6,9,8,8,4,15,9,14,5,9,7,12,6,13,8,11,6,12,6,10,8,13,9,8,5,11,4,13,7,10,5,12,6,10,5,15)
data.frame(person, cutTime)
})
# Reactive Expression: Person Cutting
cutterName <- reactive({
input$cutterPerson
})
# Reactive Expression: Filtered Data
filteredData.df <- reactive({
cookieData.df() %>% select(person, cutTime) %>% filter(person == input$cutterPerson)
})
# Output: Histogram
output$histogram <- renderPlot({
N <- nrow(filteredData.df())
binWidth = (max(filteredData.df()$cutTime)-min(filteredData.df()$cutTime) / input$binCount)
ggplot(filteredData.df(), aes(cutTime)) +
geom_histogram(aes(y = ..count..), bins = input$binCount) +
geom_density(aes(y = ..density..* N * binWidth), color = "red") + # Error: Does not recognize "N" or "binWidth"
#geom_density(aes(y = ..density.. * 20 * 1), color = "red") + # David Curve: N = 10, binWidth = (max(cutTime)-min(cutTime))/binCount = (9-4)/5 = 1
#geom_density(aes(y = ..density.. * 20 * 1.4), color = "red") + # Sharon Curve: N = 10, binWidth = (max(cutTime)-min(cutTime))/binCount = (15-8)/5 = 1.4
labs(title = "Histogram of Cookie Cut Times", x = "Cut Duration (s)", y = "Frequency") +
theme(plot.title = element_text(size = 25, face = "bold"),
axis.title = element_text(size = 15, face = "bold"))
})
# Reactive Expression: (N) Filtered Data
N <- reactive({
nrow(filteredData.df())
})
# Reactive Expression: (binWidth) Filtered Data
binWidth <- reactive({
(max(filteredData.df()$cutTime) - min(filteredData.df()$cutTime)) / input$binCount
})
}
shinyApp(ui, server)
..
calculated variables. – MrFlick