/
plot_Darwent.r
339 lines (263 loc) · 15.3 KB
/
plot_Darwent.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#' Plots participants' SLEEP/WAKE intervals.
#'
#' @param x a dataframe.
#' @param datebreaks is the distance between breaks in the x-axis. "12 hour" by default.
#' @param acolor specifies the colors for the interval_type.
#' @param decal is a parameter for shifting the start date.
#' @param export if TRUE, it will export the data as CSV.
#' @param base matches the participants at the same start date.
#' @param si defines the size of the geom_segment, 8 by default.
#' @param tz is the times zone. tz = "UTC" by default.
#' @param tz2 an additional time zone used for a secondary x-axis.
#' @param show_plot logical to produce the plot or not.
#' @param shade (deprecated) if TRUE, it will draw in light green the home night period. FALSE by default.
#' @param local.shade (deprecated) if TRUE, it will draw in gray the local night period. FALSE by default.
#' @param ... optional parameters.
#' @return a plot.
#'
#' @examples
#' library("dplyr")
#' library("lubridate")
#' data(act)
#' act %>% group_by(subject_ID) %>%
#' mutate(start = min(datime_start)) %>%
#' ungroup %>% filter(datime_start <= start + days(15), interval_type != "ACTIVE") %>%
#' plot_Darwent(., acolor = c("#D55E00", "black", "#56B4E9"),
#' tz2 = "Pacific/Auckland",
#' si = c(4, 3, 2.5))
#'
#' @export
#' @importFrom dplyr distinct left_join arrange summarize
#' @importFrom scales date_breaks date_format
#' @importFrom RColorBrewer brewer.pal
#' @importFrom lubridate is.POSIXct floor_date tz ceiling_date
#' @importFrom tidyr unnest
#' @import ggplot2
#' @import RColorBrewer
#' @rdname plot.Darwent
plot_Darwent <- function(x, datebreaks = "12 hour",
base = "TRUE", acolor, decal, export = FALSE, show_plot = TRUE,
si, tz = "UTC", tz2, shade = FALSE, local.shade = FALSE, ...){
subject_ID <- datime_start <- interval_type <- datime_end <- grayzone.start <-
grayzone.end <- dec <- HNS <- LNS <- homeTZ <- min_date <- NULL
foo <- as.data.frame(x)
foo <- foo[!is.na(foo$datime_start),]
foo <- foo[!is.na(foo$datime_end),]
if(is.POSIXct(foo$datime_start) == FALSE) {foo$datime_start <- as.POSIXct(foo$datime_start, tz = tz)}
if(is.POSIXct(foo$datime_end) == FALSE) {foo$datime_end = as.POSIXct(foo$datime_end, tz = tz)}
foo$interval_type <- as.factor(toupper(as.character(foo$interval_type)))
foo <- arrange(foo, interval_type)
dif_int <- setdiff(unique(foo$interval_type), c("ACTIVE" ,"EXCLUDED" ,"REST", "SLEEP" , "WORK", "SLEEP_B", "SLEEP_C", "DIARY", "BAD") )
if(missing(acolor)) { # Defining the color
match_color <- c("ACTIVE" = "#009E73", "EXCLUDED" = "#D55E00", "REST" = "black",
"SLEEP" = "#56B4E9", "WORK" = "#CC79A7", "SLEEP_B" = "#f03b20", "SLEEP_C" = "#F0E442", "DIARY" = "#999999", "BAD" = "#D55E00")
if(length(dif_int) > 0){
cols_ext <- brewer.pal(length(dif_int) + 2, "Paired")
names(cols_ext) <- dif_int
match_color <- c(match_color, cols_ext)
}
acolor = match_color
}
part <- length(unique(foo$subject_ID))
participant <- as.factor(unique(foo$subject_ID))
participant <- droplevels(participant)
activ <- length(unique(foo$interval_type))
if(missing(decal)){ decal = data.frame(subject_ID = participant, dec = rep(0, part))}
if(missing(homeTZ)){homeTZ = data.frame(subject_ID = participant, TZ = rep(0, length(participant)))}
if(shade == TRUE){HNS <- home.night.shade(x = foo, homeTZ = homeTZ)} #, tz = tz
if(local.shade == TRUE){LNS <- local.night.shade(localTZ = localTZ)}
match_start <- function(x){ # will match all the participants at the same start date when base = TRUE.
x2 <- x %>% group_by(subject_ID) %>%
summarize(min_date = floor_date(min(datime_start) , unit = "day")) %>%
mutate(diff = difftime(min(min_date), min_date, units = "secs")) #first
x <- x %>% left_join(x2, by = c("subject_ID" = "subject_ID"))
x$subject_ID <- factor(x$subject_ID)
x <- x %>% left_join(decal, by = c("subject_ID" = "subject_ID"))
x$dec <- x$dec * 60 * 60 * 24
x$datime_start <- x$datime_start + x$diff + x$dec
x$datime_end <- x$datime_end + x$diff + x$dec
cbind(x)
}
if(base == "TRUE"){ # matching the participants at the same start date
foo <- match_start(foo)
if(shade == TRUE){HNS <- match_start(HNS)}
if(local.shade == TRUE){LNS <- match_start(LNS)}
}
if (export == TRUE){
usef <- c("subject_ID", "datime_start", "datime_end", "interval_type")
Darw_data <- foo[, usef] %>% left_join(decal, by = "subject_ID")
write.csv(Darw_data, "Darw_data.csv")
}
foo$interval_type <- as.factor(as.character(foo$interval_type))
if(missing(si)){ si = rep(8, activ)}
p <- ggplot2::ggplot() + {
# home night shade
if(shade == TRUE) {geom_segment(data = HNS,
aes(x = as.numeric(as.POSIXct(datime_start)),
xend = as.numeric(as.POSIXct(datime_end)),
y = subject_ID, yend = subject_ID), size = 13, col = "green",
alpha = 0.22)} } + {
# local night shade
if(local.shade == TRUE) {geom_segment(data = LNS,
aes(colour = interval_type, x = as.numeric(as.POSIXct(datime_start)),
xend = as.numeric(as.POSIXct(datime_end)),
y = subject_ID, yend = subject_ID), size = 12, col = "black", alpha = 0.22)}
else localTZ <- NULL} +
geom_segment(data = foo, aes(colour = interval_type, x = as.numeric(datime_start), xend = as.numeric(datime_end),
y = subject_ID, yend = subject_ID, size = interval_type)) +
scale_size_manual(values = si) +
theme_bw() + xlab(paste("Time in", tz(foo$datime_start[1]))) + ylab("Participant(s)") +
theme_classic() +
scale_color_manual(values = acolor) +
scale_fill_manual(name = "Act")
if(show_plot == TRUE){
x_axis_lab <- seq.POSIXt(floor_date(min(foo$datime_start), unit = "day"),
ceiling_date(max(foo$datime_start), unit = "day"), by = datebreaks, tz = tz)
x_axis_lab_num <- as.integer(as.numeric(x_axis_lab))
p <- p + { if(!missing(tz2) == TRUE) scale_x_continuous(paste("Time in", tz = tz),
breaks = x_axis_lab_num,
labels = format(x_axis_lab, format = "%H:%M", tz = tz),
sec.axis = sec_axis(~ . + 0, breaks = x_axis_lab_num,
labels = format(with_tz(x_axis_lab, tzone = tz2),
format = "%H:%M"), name = paste("Time in", tz2)) ) } + {
if(missing(tz2) == TRUE) scale_x_continuous(paste("Time in", tz),
breaks = x_axis_lab_num,
labels = format(x_axis_lab, format = "%H:%M", tz = tz) ) }
p <- p + theme(axis.text.x = element_text(size = 8, angle = 90 , vjust = 0.5)) +
theme(plot.margin = unit(c(1, 0.5, 0.5, 0.5), "cm") ) +
theme(plot.title = element_text(color = "black", size = 18, hjust = 0.5)) +
theme(panel.grid.major.x = element_line(colour = 'gray', size = 0.1 )) +
ggtitle("Darwent plot")
p
}
else
p + scale_x_continuous(paste("Time in", tz), breaks = x_axis_lab_num,
labels = format(x_axis_lab, format = "%H:%M", tz = tz) ) +
theme(axis.text.x = element_text(size = 8, angle = 90 , vjust = 0.5)) +
theme(plot.margin = unit(c(1, 0.5, 0.5, 0.5), "cm") ) +
theme(plot.title = element_text(color = "black", size = 18, hjust = 0.5)) +
theme(panel.grid.major.x = element_line(colour = 'gray', size = 0.1 )) +
ggtitle("Darwent plot")
}
#' Plots with-in participant activity intervals (SLEEP, REST, ACTIVE, etc) in 24 hour sections.
#'
#' @param dat a dataframe.
#' @param acolor the color of the lines.
#' @param si defines the size of the geom_segment, 1.25 by default.
#' @param tz the time zone.
#' @param tz2 an additional time zone.
#' @param sp the starting point in the x-axis. Set as 00:00:00 by default.
#' @param with_date allows adding the cutpoint date to the y-axis. with_date = FALSE by default.
#' @param alphas is the transparency.
#' @param hourbreaks is the number of labels on the x-axis.
#' @param ... Optional parameters.
#' @return a plot.
#'
#'
#'@details The date-time data in the data frame dat will keep time zone.
#' The argument tz and tz2 are used to specify the time in the x-axis.
#'
#' @examples
#' library("dplyr")
#' data(act)
#' act[act$subject_ID == 1 & act$interval_type != "ACTIVE",] %>%
#' plot_long(., si = c(3, 2.5, 2, 1.5),
#' tz2 = "Pacific/Auckland",
#' acolor = c("#D55E00", "black", "#56B4E9"))
#' @export
#' @importFrom dplyr distinct
#' @importFrom scales date_breaks date_format
#' @importFrom lubridate seconds is.POSIXct floor_date tz ceiling_date
#' @importFrom tidyr unnest
#' @import ggplot2
#' @rdname plot_long
plot_long <- function(dat, acolor, si, tz = "UTC", tz2, sp = "00:00:00", with_date = FALSE, alphas = 1, hourbreaks = 5, ...) {
sinceMidnight_end <- sinceMidnight_start <- interval_type <- dat3 <- dat4 <- dat5 <- dat6 <- NULL
dat <- dat[!is.na(dat$datime_start),]
dat <- dat[!is.na(dat$datime_end),]
dat <- arrange(dat, interval_type)
dat$subject_ID <- gsub("_.*$", "", dat$subject_ID)
if(is.POSIXct(dat$datime_start) == FALSE) {dat$datime_start <- as.POSIXct(dat$datime_start, tz = tz)}
if(is.POSIXct(dat$datime_end) == FALSE) {dat$datime_end = as.POSIXct(dat$datime_end, tz = tz)}
# make time into mins since midnight - make the sleeps that cross midnight plot across 2 lines
dat$trun_start <- as.POSIXct(paste(trunc(dat$datime_start, "day"), sp),format = "%Y-%m-%d %H:%M:%S", tz = tz(dat$datime_start))
dat$trun_end <- as.POSIXct(paste(trunc(dat$datime_end, "day"), sp),format = "%Y-%m-%d %H:%M:%S", tz = tz(dat$datime_start))
# extract the date and make into factor counter: This is not working for cases where the REST/SLEEP occurs after midnight
df_seq <- data.frame(seqDates = seq.POSIXt(min(dat$trun_start), max(dat$trun_end ), by = "day"),
sequence = 1 : length(seq.POSIXt(min(dat$trun_start), max(dat$trun_end), by = "day")))
dat <- dat %>% left_join(df_seq, c("trun_start" = "seqDates"))
dat$sinceMidnight_start <- as.numeric(difftime(dat$datime_start, dat$trun_start , units = "mins"))
dat$sinceMidnight_end <- as.numeric(difftime(dat$datime_end, dat$trun_end , units = "mins")) #trunc(dat$datime_end, "day")
dat[dat$sinceMidnight_start < 0,]$sequence <- dat[dat$sinceMidnight_start < 0,]$sequence - 1
# adjusting the negative intervals.
if(nrow(filter(dat, sinceMidnight_start < 0)) > 0){
dat2 <- filter(dat, sinceMidnight_start < 0)
if(nrow(filter(dat2, sinceMidnight_end <= 0)) > 0) {
dat3 <- filter(dat2, sinceMidnight_end <= 0)
dat3$sinceMidnight_start <- dat3$sinceMidnight_start + 1440
dat3$sinceMidnight_end <- dat3$sinceMidnight_end + 1440 }
if(nrow(filter(dat2, sinceMidnight_end > 0)) > 0) {
dat4 <- filter(dat2, sinceMidnight_end > 0)
dat5 <- dat4
dat5$sinceMidnight_start = 1440 + dat5$sinceMidnight_start
dat5$sinceMidnight_end = 1440
dat4$sinceMidnight_start <- 0
dat4$sequence <- dat4$sequence + 1 }
dat6 <- rbind(dat3, dat4, dat5)
dat <- dat[dat$sinceMidnight_start >= 0,]
dat <- rbind(dat, dat6)
}
dat[dat$sinceMidnight_start > 0 & dat$sinceMidnight_end < 0,]$sinceMidnight_end <- 1440 +
dat[dat$sinceMidnight_start > 0 & dat$sinceMidnight_end < 0,]$sinceMidnight_end
dat <- arrange(dat, interval_type)
activ <- length(unique(dat$interval_type))
if(missing(si)){ si = rep(1.25, activ)}
dif_int <- setdiff(unique(dat$interval_type), c("ACTIVE" ,"EXCLUDED" ,"REST", "SLEEP" , "WORK", "SLEEP_B", "SLEEP_C", "DIARY", "BAD") )
if(missing(acolor)) { # Defining the color
match_color <- c("ACTIVE" = "#009E73", "EXCLUDED" = "#D55E00", "REST" = "black",
"SLEEP" = "#56B4E9", "WORK" = "#CC79A7", "SLEEP_B" = "#f03b20", "SLEEP_C" = "#F0E442", "DIARY" = "#999999", "BAD" = "#D55E00")
if(length(dif_int) > 0){
cols_ext <- brewer.pal(length(dif_int) + 2, "Paired")#[1 : length(dif_int)]
names(cols_ext) <- dif_int
match_color <- c(match_color, cols_ext)
}
acolor = match_color
}
p <- ggplot(data = dat)
# plot the x-midnight segments in 2 goes: one for the bit between sleep start and midnight
if(nrow(dat %>% filter(sinceMidnight_start >= 0 & sinceMidnight_end > 0 & sinceMidnight_end < sinceMidnight_start)) > 0){
p <- p + geom_segment(aes(colour = interval_type, x = sinceMidnight_start, xend = 1440, y = sequence, yend = sequence, size = interval_type), alpha = alphas, data = dat %>% filter(sinceMidnight_start >= 0 & sinceMidnight_end > 0 & sinceMidnight_end < sinceMidnight_start))
p <- p + geom_segment(aes(colour = interval_type, x = 0, xend = sinceMidnight_end, y = sequence + 1, yend = sequence + 1, size = interval_type), alpha = alphas, data = dat %>% filter(sinceMidnight_start >= 0 & sinceMidnight_end > 0 & sinceMidnight_end < sinceMidnight_start))}
if(nrow(dat %>% filter(sinceMidnight_start >= 0 & sinceMidnight_end > 0 & sinceMidnight_end > sinceMidnight_start)) > 0){
p <- p + geom_segment(aes(colour = interval_type, x = sinceMidnight_start, xend = sinceMidnight_end, y = sequence, yend = sequence, size = interval_type, alpha = alphas), alpha = alphas , data = dat %>% filter(sinceMidnight_start >= 0 & sinceMidnight_end > 0 & sinceMidnight_end > sinceMidnight_start))}
p <- p + scale_size_manual(values = si)
# flip it upside down so newest dates are on the top of the figure
p <- p + theme_bw()
p <- p + ggtitle(gsub("_.*$", "", dat$subject_ID))
p <- p + labs(x = "Time", y = "Days")
b <- seq.POSIXt(dat$trun_start[1], dat$trun_start[1] + hours(24), length.out = hourbreaks) #c("00:00", "06:00", "12:00", "18:00", "24:00")
b <- format(b, format="%H:%M", usetz = TRUE, tz = tz)
if(missing(tz2)){labs = b}
else{ b2 <- strptime(paste("01-01-2000", b), format= "%d-%m-%Y %H:%M", tz = tz) #"EST"
b2EST <- with_tz(b2, tzone = tz2)
hmEST <- format(b2EST, format="%H:%M", usetz = TRUE)
labs <- paste(b, hmEST, sep = "\n")
}
p <- p + scale_x_continuous(limits = c(0, 1440), breaks = seq(0, 1440, 360),
expand = c(0, 0),labels = labs)
# adding date info in y axis
bys <- unique(df_seq$sequence)
if(with_date==FALSE ){labs2 = bys}
else{labs2 <- paste(bys, unique(df_seq$seqDates), sep = " ") }
p <- p + scale_y_reverse(breaks = unique(df_seq$sequence) , labels = labs2)
p <- p + scale_color_manual(values = acolor)
p <- p + theme(
strip.background = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
plot.margin = unit(c(5.5, 12.5, 5.5, 5.5), "points"),
axis.title.y = element_text(size = 10, colour = "black", face = "bold", margin = margin(0, 0, 0, 0, unit = "points")),
axis.title.x = element_text(size = 10, colour = "black", face = "bold", margin = margin(10, 0, 0, 0, unit = "points")),
axis.text = element_text(size = 10, colour = "black", face = "bold"))
p
}