Skip to content

Commit

Permalink
added option to select different forms for the plots
Browse files Browse the repository at this point in the history
  • Loading branch information
B.Anderson committed Jul 6, 2020
1 parent 93a9c42 commit 9669418
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
17 changes: 11 additions & 6 deletions R/createDailyMeanComparePlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param dt the data, assumed to be the aligned data (use alignDates() to do this)
#' @param yVar the variable you want to plot
#' @param yCap the caption for the y axis
#' @param form do you want a "line" = geom_line() or a "step" = geom_step()? Step is the default
#' @param yDiv the value you want to divide yVar by to make the y axis more sensible. Default = 1
#' @param lockDownStart date for start of lockdown rectangle annotation
#' @param lockDownEnd date for end of lockdown rectangle annotation
Expand All @@ -18,7 +19,7 @@
#' @export
#' @family plot
#'
createDailyMeanComparePlot <- function(dt, yVar, yCap, yDiv = 1, lockDownStart, lockDownEnd){
createDailyMeanComparePlot <- function(dt, yVar, yCap, form = "step", yDiv = 1, lockDownStart, lockDownEnd){
# assumes the dateFixed half-hourly data
# assumes we want mean of half-hourly obs
plotDT <- dt[dateFixed <= lubridate::today() &
Expand All @@ -37,12 +38,9 @@ createDailyMeanComparePlot <- function(dt, yVar, yCap, yDiv = 1, lockDownStart,
shape = weekDay,
colour = compareYear)) +
geom_point() +
geom_line(aes(shape = NULL), linetype = "dashed") + # joint the dots within compareYear
scale_x_date(date_breaks = "7 day", date_labels = "%a %d %b") +
theme(axis.text.x=element_text(angle=90, hjust=1)) +
labs(caption = paste0(localParams$lockdownCap, localParams$weekendCap,
"\n", localParams$loessCap),
x = "Date",
labs(x = "Date",
y = yCap
) +
theme(legend.position = "bottom") +
Expand All @@ -51,7 +49,14 @@ createDailyMeanComparePlot <- function(dt, yVar, yCap, yDiv = 1, lockDownStart,
scale_shape_discrete(name = "Weekday") +
guides(colour=guide_legend(nrow=2)) +
guides(shape=guide_legend(nrow=2))

if(form == "line"){
p <- p + geom_line(aes(shape = NULL),
linetype = "dashed") # join the dots within compareYear
}
if(form == "step"){
p <- p + geom_step(aes(shape = NULL),
linetype = "dashed") # join the dots within compareYear
}
p <- addLockdownRect(p,
from = lockDownStart,
to = lockDownEnd,
Expand Down
66 changes: 45 additions & 21 deletions R/makeWeekdayTimePlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#'
#' @param dt the data
#' @param yVar the variable you want to plot
#' @param yForm whether you want an abs(olute) (default) or a prop(ortional) plot
#' @param yLab the label for the y axis
#' @param yDiv the value you want to divide yVar by to make the y axis more sensible. Default = `1`
#'
Expand All @@ -16,26 +17,49 @@
#' @export
#' @family plot
#'
makeWeekdayTimePlot <- function(dt, yVar, yLab, yDiv){
makeWeekdayTimePlot <- function(dt, yVar, yForm = "abs", yLab = "y Lab", yDiv = 1){
# by weekday and hour
# use proportion to show relative shifts
# wkdayFixed = obs (they are the same - that was the whole idea!)
dt <- dt[, .(yVals = mean(get(yVar))/yDiv), keyby = .(hms, plotPeriod, compareYear, wkdayFixed)]
sums <- dt[, .(sum = sum(yVals)), keyby = .(compareYear, plotPeriod, wkdayFixed)]
setkey(sums, compareYear, plotPeriod, wkdayFixed)
setkey(dt, compareYear, plotPeriod, wkdayFixed)
mDT <- sums[dt]
mDT[, pVal := (yVals/sum)*100]
p <- ggplot2::ggplot(mDT, aes(x = hms, y = pVal,
colour = compareYear)) +
#geom_line() +
geom_point() +
scale_x_time(labels = NULL) +
#scale_x_datetime(breaks=date_breaks('4 hour'),labels=date_format('%H:%M')) +
theme(legend.position="bottom") +
scale_color_discrete(name="Year") +
facet_grid(plotPeriod ~ wkdayFixed ) +
labs( y = yLab,
x = "Time")
return(p)
if(yForm == "prop"){
# use proportion to show relative shifts
# wkdayFixed = obs (they are the same - that was the whole idea!)
# use month as the facet to link to other plots
dt[, month := lubridate::month(dateFixed, label = TRUE)]
dt <- dt[, .(pVals = mean(get(yVar))/yDiv),
keyby = .(hms, month, compareYear, wkdayFixed)]
sums <- dt[, .(sum = sum(pVals)), keyby = .(compareYear, month, wkdayFixed)]
setkey(sums, compareYear, month, wkdayFixed)
setkey(dt, compareYear, month, wkdayFixed)
plotDT <- sums[dt]
plotDT[, yVal := (pVals/sum)*100]
ok <- TRUE
}
if(yForm == "abs"){
#message("abs")
dt[, month := lubridate::month(dateFixed, label = TRUE)]
plotDT <- dt[, .(yVal = mean(get(yVar))/yDiv),
keyby = .(hms, month, compareYear, wkdayFixed)]
ok <- TRUE
}
if(yForm != "abs" & yForm != "prop"){# be explicit
# neither
e <- "yForm not recognised - function understands 'abs' or 'prop"
return(e)
}
if(ok){
#message("Building plot with ", yForm)
p <- ggplot2::ggplot(plotDT, aes(x = hms, y = yVal,
colour = compareYear)) +
geom_step() +
#geom_point(size = 1, stroke = 1, shape = 16) +
#scale_x_time(labels = NULL) +
#scale_x_time(date_format('%H:%M'))
#scale_x_datetime(breaks=date_breaks('4 hour'),labels=date_format('%H:%M')) +
theme(axis.text.x=element_text(angle=90, hjust=1)) +
theme(legend.position="bottom") +
scale_color_discrete(name="Year") +
facet_grid(month ~ wkdayFixed ) +
labs( y = yLab,
x = "Time")
return(p)
}
}

0 comments on commit 9669418

Please sign in to comment.