Skip to content

Instantly share code, notes, and snippets.

@pssguy
Last active January 21, 2018 01:25
Show Gist options
  • Save pssguy/5059329 to your computer and use it in GitHub Desktop.
Save pssguy/5059329 to your computer and use it in GitHub Desktop.
Motion Chart of Premier League Positions by game for past 20+ seasons. Shiny app using googleVis package The data is not currently provided but to-date charts can be viewed at glimmer.rstudio.com/pssguy/eplTableMotion/
# load requisite libraries
library(shiny)
library(googleVis)
# load raw data (NB not provided)
tableByGame <- read.csv("../tableByGame.csv",stringsAsFactors=FALSE)
# to create chart need to repeat one column and get negative of league position as hack
tableByGame$game <- tableByGame$seasonGame
tableByGame$lgPos <- -tableByGame$lgPos
# set meaningful column names
colnames(tableByGame) <- c("Team","Season","Res","Pl","Points","GD","GF","Position","Games")
# list seasons available for selection
seasonChoice <- unique(tableByGame$Season)
shinyServer(function(input, output) {
# use the new renderGvis
output$gvMotion <- renderGvis({
# subset by season
dat <- subset(tableByGame,Season==input$season1)
# set initial conditions
initState <- '
{"iconKeySettings":[{"trailStart":"1901","key":{"dim0":"Man. Utd."}},
{"trailStart":"1901","key":{"dim0":"West Brom"}},{"trailStart":"1901",
"key":{"dim0":"Liverpool"}}],"orderedByX":false,"yZoomedDataMin":-20,
"yZoomedDataMax":-1,"dimensions":{"iconDimensions":["dim0"]},
"showTrails":true,"time":"1901","yLambda":1,"xAxisOption":"2",
"nonSelectedAlpha":0.1,"xZoomedDataMin":1,"yZoomedIn":false,
"playDuration":30000,"xZoomedIn":false,"iconType":"BUBBLE",
"xLambda":1,"colorOption":"_UNIQUE_COLOR","sizeOption":"7",
"duration":{"timeUnit":"Y","multiplier":1},"xZoomedDataMax":27,
"uniColorForNonSelected":false,"yAxisOption":"3","orderedByY":false}'
# produce chart
gvisMotionChart(dat, idvar="Team", timevar="Games",xvar="Pl", yvar="Position", options=list(state=initState))
})
})
shinyUI(pageWithSidebar(
# Application title
headerPanel("EPL Motion Tables"),
# Sidebar with controls to select the relevant team/season/games played
# Options vary with tab selected
sidebarPanel(
helpText("Recreate how any of the EPL seasons unfolded by selecting a year and pressing
the play button. Amend highlighted teams, speed of motion etc. as desired"),
helpText("Based on the wonderful Shiny and googleVis R packages"),
selectInput("season1", label="Select Season:",choices=seasonChoice,selected="2012/2013",multiple=FALSE),
br(),
p("Comprehensive EPL Data - ",
a("PremierSoccerStats", href="http://www.premiersoccerstats.com")
),
p("Regular Articles - ",
a("PSS blog", href="http://premiersoccerstats.com/wordpress/")
),
p("Twitter Feed - ",
a("@pssguy", href="http://twitter.com/pssGuy")
)
),
mainPanel(
tableOutput("gvMotion")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment