-
Notifications
You must be signed in to change notification settings - Fork 3
/
createDailyPcComparePlot.R
65 lines (60 loc) · 2.83 KB
/
createDailyPcComparePlot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#' Creates a daily mean half-hourly % difference plot from the data.table given
#'
#' `createDailyPcComparePlot` returns a plot which calculates the daily mean of yVar and plots the % difference between the value for 2020 and
#' 2017-2019 to compare the values for the same date over previous years. This assumes you pass in the aligned-date data.
#'
#' @param dt the data, assumed to be the aligned data (use alignDates() to create this)
#' @param yVar the variable you want to plot
#' @param yCap the caption for the y axis
#' @param lockDownStart date for start of lockdown rectangle annotation
#' @param lockDownEnd date for end of lockdown rectangle annotation
#'
#' @import lubridate
#' @import data.table
#' @author Ben Anderson, \email{b.anderson@@soton.ac.uk}
#' @export
#' @family plot
#'
createDailyPcComparePlot <- function(dt, yVar, yCap, lockDownStart, lockDownEnd){
# assumes the dateFixed half-hourly data
# assumes we want mean of half-hourly obs
#yVar = "consGWh",
#yCap = "% difference",
dt <- dt[dateFixed <= lubridate::today() &
dateFixed >= localParams$comparePlotCutDate] # make this lopnger to get a sense of trend
dt[, compareVar := get(yVar)]
baseDT <- dt[compareYear != "2020", .(baseMean = mean(compareVar)), # careful - uses compareYear label!!
keyby = .(dateFixed, wkdayFixed, compareYear)]
testDT <- dt[compareYear == "2020", .(testMean = mean(compareVar)),
keyby = .(dateFixed, wkdayFixed, compareYear)]
setkey(baseDT, dateFixed, wkdayFixed)
setkey(baseDT, dateFixed, wkdayFixed)
plotDT <- baseDT[testDT] # auto drops non matches to 2020
plotDT[, pcDiffMean := 100*(testMean - baseMean)/baseMean] # -ve value indicates lower
plotDT[, pos := ifelse(pcDiffMean > 0 , "Pos", "Neg")] # want to colour the line sections - how?
# final plot - adds annotations
# use means for consistency with comparison plots where we use WHO thresholds (means)
yMin <- min(plotDT$pcDiffMean, na.rm = TRUE)
print(paste0("Max drop %:", round(yMin)))
yMax <- max(plotDT$pcDiffMean, na.rm = TRUE)
print(paste0("Max increase %:", round(yMax)))
p <- ggplot2::ggplot(plotDT, aes(x = dateFixed, y = pcDiffMean
#color = pos, group=NA)
)
) +
geom_step() +
scale_x_date(date_breaks = "7 days", date_labels = "%a %d %b") +
theme(axis.text.x=element_text(angle=90, hjust=1, size = 5)) +
labs(x = "Date",
y = yCap,
caption = paste0(localParams$lockdownCap, localParams$weekendCap)) +
theme(legend.position="bottom") +
geom_hline(yintercept = 0, linetype = 3)
p <- addLockdownRect(p,
from = lockDownStart,
to = lockDownEnd,
yMin = yMin,
yMax = yMax)
p <- addWeekendRectsDate(p, yMin, yMax)
return(p)
}