-
Notifications
You must be signed in to change notification settings - Fork 3
/
createDailyMeanComparePlot.R
77 lines (74 loc) · 3.33 KB
/
createDailyMeanComparePlot.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
66
67
68
69
70
71
72
73
74
75
76
77
#' Creates a daily half-hourly mean plot from the data.table given
#'
#' `createDailyMeanComparePlot` returns a plot which calculates the mean of yVar and plots it for 2020 and 2017-2019 to
#' compare the values for the same date over previous years. This assumes users pass in the aligned-date data (`dateFixed`).
#'
#' Adds a smoothed line using loess.
#'
#' @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
#' @param comparePlotCut start date to cut the plot (default 2020-01-01)
#' @param toDate date to end the plot (usually the most recent observation), default = today()
#'
#' @import lubridate
#' @import data.table
#' @author Ben Anderson, \email{b.anderson@@soton.ac.uk}
#' @export
#' @family plot
#'
createDailyMeanComparePlot <- function(dt, yVar, yCap, form = "step", yDiv = 1,
lockDownStart, lockDownEnd,
comparePlotCut = as.Date("2020-01-01"),
toDate = lubridate::today()){
message("lockDownStart: ", lockDownStart)
message("lockDownEnd: ", lockDownEnd)
message("toDate: ", toDate)
# assumes the dateFixed half-hourly data
# assumes we want mean of half-hourly obs
plotDT <- dt[dateFixed >= comparePlotCut & dateFixed <= lubridate::as_date(toDate), # otherwise we get the whole year
.(yMean = mean(get(yVar))/yDiv, # do this here so min/max work
nObs = .N
),
keyby = .(dateFixed, wkdayFixed, weekDay, compareYear)]
# make plot - adds annotations
yMin <- min(plotDT$yMean)
yMax <- max(plotDT$yMean)
p <- ggplot2::ggplot(plotDT, aes(x = dateFixed,
y = yMean,
shape = weekDay,
colour = compareYear)) +
geom_point() +
scale_x_date(date_breaks = "7 day", date_labels = "%a %d %b") +
theme(axis.text.x=element_text(angle=90, hjust=1)) +
labs(x = "Date",
y = yCap
) +
theme(legend.position = "bottom") +
geom_smooth(aes(shape = NULL), method = "loess") + # will get a smooth line per year not per day, force loess not gam
scale_color_discrete(name = "Period") +
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,
yMin = yMin,
yMax = yMax)
p <- addWeekendRectsDate(p,
yMin,
yMax)
return(p)
}