Skip to content

Commit

Permalink
two more graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
ha292 committed Apr 22, 2015
1 parent becacac commit f57e737
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
35 changes: 35 additions & 0 deletions shiny_propotype/v3/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,45 @@ getTimingPlot2 <- function(ds) {
g
}

getTimingPlot3 <- function(ds) {
a <- ds$accidents
a$qtr <- factor(quarters(a$date))
df <- a %>% group_by(qtr, HOUR) %>% summarize(fatalities=sum(FATALS))
df <- filter(df, HOUR < 24)
g <- ggplot(df, aes(x=HOUR, y=fatalities, color=qtr, group=qtr)) +
geom_line() +
theme_bw() +
theme(legend.key = element_blank()) +
theme(legend.title = element_blank()) +
xlab('Hour of day') +
ylab('Fatalities')
g
}

getTimingPlot4 <- function(ds) {
a <- ds$accidents
whour <- as.POSIXlt(a$date)$wday * 24 + a$HOUR
a$qtr <- factor(quarters(a$date))
df <- data.frame(a, whour) %>% group_by(whour, qtr) %>% summarize(fatalities=sum(FATALS))
df <- filter(df, whour < 168)
# We want to show actual weekday on the x axis so we have to resort to a trick. 2/1/2015 is a Sunday.
df$t <- as.POSIXct('2015-02-01') + df$whour * 3600

g <- ggplot(df, aes(x=t, y=fatalities, color=qtr, group=qtr)) +
geom_line() +
theme_bw() +
theme(legend.key = element_blank()) +
theme(legend.title = element_blank()) +
scale_x_datetime(labels=date_format("%I%P\n%a"), breaks=date_breaks("12 hours")) + xlab('') +
ylab('Fatalities')
g
}
shinyServer(function(input, output) {
ds <- loadFatalityDataset(2013, '../../')
output$timingPlot1 <- renderPlot({getTimingPlot1(ds)})
output$timingPlot2 <- renderPlot({getTimingPlot2(ds)})
output$timingPlot3 <- renderPlot({getTimingPlot3(ds)})
output$timingPlot4 <- renderPlot({getTimingPlot4(ds)})
})


8 changes: 6 additions & 2 deletions shiny_propotype/v3/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dashboardPage(
dashboardSidebar(
sidebarMenu(
menuItem("General summary", tabName="summary_tab", icon=icon("car")),
menuItem("Timing", tabName="timing_tab", icon=icon("line-chart")),
menuItem("Timing", tabName="timing_tab", icon=icon("calendar")),
menuItem("Alcohol", tabName="alco_tab", icon=icon("beer")),
menuItem("Children", tabName="children_tab", icon=icon("child")),
menuItem("Bikes", tabName="bike_tab", icon=icon("bicycle")),
Expand Down Expand Up @@ -44,7 +44,11 @@ tabItems(
h3('All 2013 Fatalities by weekday'),
plotOutput("timingPlot1"),
h3('All 2013 Fatalities by weekday and State'),
plotOutput("timingPlot2")
plotOutput("timingPlot2"),
h3('Time of day'),
plotOutput("timingPlot3"),
h3('Over the course of the week'),
plotOutput("timingPlot4")
)
),

Expand Down

0 comments on commit f57e737

Please sign in to comment.