-
-
Save sjengle/9d5152f0fd4e86d31448 to your computer and use it in GitHub Desktop.
MSAN 622 Shiny Animated Time Series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sophie Engle | |
# CS 360/MSAN 622 Data Visualization | |
# University of San Francisco | |
require(ggplot2) | |
require(shiny) | |
require(reshape) | |
require(scales) | |
data(UKLungDeaths) | |
series <- as.numeric(time(ldeaths)) | |
deaths <- as.numeric(ldeaths) | |
male <- as.numeric(mdeaths) | |
female <- as.numeric(fdeaths) | |
lungdata <- data.frame(series, deaths, male, female) | |
lungmelt <- melt(lungdata, id = "series") | |
grey <- "#dddddd" | |
plotOverview <- function(start = 1974, num = 12) { | |
xmin <- start | |
xmax <- start + (num / 12) | |
ymin <- 1200 | |
ymax <- 4000 | |
p <- ggplot(lungdata, aes(x = series, y = deaths)) | |
p <- p + geom_rect( | |
xmin = xmin, xmax = xmax, | |
ymin = ymin, ymax = ymax, | |
fill = grey) | |
p <- p + geom_line() | |
p <- p + scale_x_continuous( | |
limits = range(series), | |
expand = c(0, 0), | |
breaks = seq(1974, 1980, by = 1)) | |
p <- p + scale_y_continuous( | |
limits = c(ymin, ymax), | |
expand = c(0, 0), | |
breaks = seq(ymin, ymax, length.out = 3)) | |
p <- p + theme(panel.border = element_rect( | |
fill = NA, colour = grey)) | |
p <- p + theme(axis.title = element_blank()) | |
p <- p + theme(panel.grid = element_blank()) | |
p <- p + theme(panel.background = element_blank()) | |
return(p) | |
} | |
plotArea <- function(start = 1974, num = 12) { | |
xmin <- start | |
xmax <- start + (num / 12) | |
ymin <- 0 | |
ymax <- 4000 | |
p <- ggplot( | |
subset(lungmelt, variable != "deaths"), | |
aes(x = series, y = value, | |
group = variable, | |
fill = variable)) | |
p <- p + geom_area() | |
minor_breaks <- seq( | |
floor(xmin), | |
ceiling(xmax), | |
by = 1/ 12) | |
p <- p + scale_x_continuous( | |
limits = c(xmin, xmax), | |
expand = c(0, 0), | |
oob = rescale_none, | |
breaks = seq(floor(xmin), ceiling(xmax), by = 1), | |
minor_breaks = minor_breaks) | |
p <- p + scale_y_continuous( | |
limits = c(ymin, ymax), | |
expand = c(0, 0), | |
breaks = seq(ymin, ymax, length.out = 5)) | |
p <- p + theme(axis.title = element_blank()) | |
p <- p + theme( | |
legend.text = element_text( | |
colour = "white", | |
face = "bold"), | |
legend.title = element_blank(), | |
legend.background = element_blank(), | |
legend.direction = "horizontal", | |
legend.position = c(0, 0), | |
legend.justification = c(0, 0), | |
legend.key = element_rect( | |
fill = NA, | |
colour = "white", | |
size = 1)) | |
return(p) | |
} | |
# test_start = 1979.35 | |
# print(plotOverview(start = test_start)) | |
# print(plotArea(start = test_start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sophie Engle | |
# CS 360/MSAN 622 Data Visualization | |
# University of San Francisco | |
shinyServer(function(input, output) { | |
output$mainPlot <- reactivePlot(function() { | |
print(plotArea(input$start, input$num)) | |
}) | |
output$overviewPlot <- reactivePlot(function() { | |
print(plotOverview(input$start, input$num)) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sophie Engle | |
# CS 360/MSAN 622 Data Visualization | |
# University of San Francisco | |
shinyUI(pageWithSidebar( | |
headerPanel("UK Deaths from Lung Diseases"), | |
sidebarPanel( | |
sliderInput( | |
"num", | |
"Months:", | |
min = 4, | |
max = 24, | |
value = 12, | |
step = 1), | |
sliderInput( | |
"start", | |
"Starting Point:", | |
min = 1974, | |
max = 1980, | |
value = 1974, | |
step = 1 / 12, | |
round = FALSE, | |
ticks = TRUE, | |
format = "####.##", | |
animate = animationOptions( | |
interval = 800, | |
loop = TRUE) | |
) | |
), | |
mainPanel( | |
plotOutput( | |
outputId = "mainPlot", | |
width = "100%", | |
height = "400px"), | |
plotOutput( | |
outputId = "overviewPlot", | |
width = "100%", | |
height = "200px") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment