Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Created August 15, 2019 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmackintosh/172e74470dfc321ecf3ca10029d47c1b to your computer and use it in GitHub Desktop.
Save johnmackintosh/172e74470dfc321ecf3ca10029d47c1b to your computer and use it in GitHub Desktop.
trial run with NHSRdatasets package and runcharter
library(NHSRdatasets) # install from github for latest dataset
library(runcharter)
library(data.table)
library(magrittr)
# Load
data("ae_attendances")
DT <- setDT(ae_attendances)
# wrangle
DT1 <- DT[order(period),][type == 1,] # filter for type == 1 and order by date
setnames(DT1, old = c("period","org_code"),new = c("date","grp")) #rename cols
DT1[,y := (attendances),] # create new column, in this case copying attendances
# this keeps the DT flexible so we can group by another metric later
# runcharter - returns a list with plot and data frame ("sustained").
attends <- runcharter(DT1,med_rows = 9, direction = "above")
# uncomment to show the plot
# attends$runchart
# whoa 146 charts!!
# there are too many locations for my laptop screen, so let's cut it down a bit.
# we want to only plot areas where signals occur
# so get a list of unique groups (org_codes) from the sustained data frame
changes <- unique(attends$sustained$grp)
# take the runchart data and use that for filtering
attends <- setDT(attends$runchart$data) # make sure it's a data.table
attends <- attends[grp %chin% changes,] #filter it for locations with signals
# the dataset now has extra columns that might cause a problem when plotting
# with runcharter, so let's just take what we need
# columns we want:
selected <- c("grp","date","y")
# SDcols is a subset of the data.table
# basically all the rows, .SD = subset the data, then specify the SD columns
attends <- attends[ , .SD, .SDcols = selected]
# now analyse and plot the filtered dataset with runcharter
p <- runcharter(attends, med_rows = 9, direction = "above",
chart_title = "Signals demonstrating rise in A&E Attendances",
facet_cols = 5)
p <- p$runchart + ggplot2::facet_wrap( ~ grp,
scales = "free_y",
ncol = 5)
p
# now down to 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment