-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCal_Actogram.R
169 lines (140 loc) · 5.29 KB
/
Cal_Actogram.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
## Cal_Actogram: Double-plotted actogram -------------------------------------
# TO DO:
# - fix naming convention issue
# - figure out why df.long from merged file doesn't work
# -
library(tidyverse)
library(ggnewscale)
## Define filename/path ------------
filename <- "2023-10-31_cal023_Clean.Rda"
fpath <- "C:/Users/kaspe/Barrow Neurological Institute Dropbox/Chelsea Faber/Mirzadeh Lab Dropbox MAIN/Data/Calorimetry/macro_processed/r_cleaned/"
groupvar <- "Treatment"
activity <- "AllMeters" # default to AllMeters
plt <- "Dark2"
export <- F
ftype <- ".pdf" # default
## Load data and extract activity into separate df --------------
load(paste0(fpath,filename))
unitkeys <- read_csv(paste(fpath,"Cal_Units.csv",sep="/"))
if (export) {
repo <- paste(rundate,cohort,"plots",sep="_")
repo <- paste0(fpath,"/",repo,"/actograms")
if (!dir.exists(repo)) {
dir.create(repo)
}
}
## Extract activity df -----------------------------------------------------
acto <- df %>%
select(DateTime:Cage,XBreak_R,YBreak_R,AllMeters,EE,LightCycle) %>%
group_by(ExpDay) %>%
mutate(Time_daily = as.numeric(
difftime(DateTime,first(DateTime)), units = "hours"),.after = DateTime) %>%
ungroup() %>%
mutate(facet_x = 1,
facet_y = ExpDay) %>%
mutate(Photoperiod_actual = case_when(
LightCycle == "LD" ~ Photoperiod,
LightCycle == "DD" ~ "Dark"))
acto_dups <- acto %>%
filter(ExpDay != 1) %>%
mutate(
facet_x = 2,
facet_y = facet_y - 1
)
acto <- bind_rows(acto,acto_dups)
rm(acto_dups)
# Extract photoperiod df
pp <- acto %>% distinct(Time_daily,ZT,Photoperiod,Photoperiod_actual)
## Heat map style actogram -------------
ggplot(filter(acto,Animal == "dtx024"),
aes(x = Time_daily,
y = ExpDay,
fill = .data[[activity]])) +
geom_raster(hjust = 0, vjust = 0.5) +
scale_fill_continuous(low = "white", high = "red4") +
#new_scale_fill() +
#geom_tile(data = pp,
# mapping = aes(x = Time_daily, fill = Photoperiod,y=1),
# linewidth = 0,
# alpha = 0.3,
# linetype = 0,
# height = Inf, # tiles will go all the way up and down
# show.legend = NA,
# inherit.aes = FALSE) +
#scale_fill_manual(values = c("Dark" = "gray45",
# "Light" = "white"),guide = "none") +
theme_classic() +
scale_x_continuous(limits = c(0,24),
name = "Zeitgeber Time",
expand = expansion(0)) +
scale_y_reverse() +
scale_y_continuous(breaks = seq(length(unique(acto$ExpDay)),1),
expand = expansion(0,0))
## Double-plotted actogram -------------
# Loop through all animals
actograms <- vector(mode = "list", length = n_distinct(acto$Animal))
for (ani in 1:n_distinct(acto$Animal)) {
animal <- as.character(unique(acto$Animal)[ani])
names(actograms)[ani] <- animal
actograms[[ani]] <- acto %>% filter(Animal == animal) %>%
ggplot() +
geom_tile(aes(x = Time_daily, y = 1, fill = Photoperiod_actual),
linewidth = 1,
alpha = 0.3,
color = "black",
linetype = 0,
height = Inf, # tiles will go all the way up and down
show.legend = NA,
inherit.aes = FALSE) +
scale_fill_manual(values = c("Dark" = "gray45",
"Light" = "white"),guide = "none") +
geom_col(mapping = aes(x = Time_daily,y = .data[[activity]]),
fill = "black",
show.legend = FALSE) +
geom_hline(
aes(yintercept = 0),
color = "black") +
# Dark cycle box
geom_rect(
aes(xmin = 0, xmax = 12, ymin = ymin, ymax = ymax),
color = "black",
fill = "black",
inherit.aes = FALSE,
data = . %>%
filter(ExpDay == 1) %>%
summarize(ymin = max(.data[[activity]]) + .25*max(.data[[activity]]),
ymax = ymin + .25*max(.data[[activity]]),
facet_y = 1)) +
# Light cycle box
geom_rect(
aes(xmin = 12, xmax = 24, ymin = ymin, ymax = ymax),
color = "black",
fill = "white",
inherit.aes = FALSE,
data = . %>%
filter(ExpDay == 1) %>%
summarize(ymin = max(.data[[activity]]) + .25*max(.data[[activity]]),
ymax = ymin + .25*max(.data[[activity]]),
facet_y = 1)) +
facet_grid(
facet_y ~ facet_x,
switch = "y",
labeller = labeller(facet_y = ~ paste("Day", 1 : max(acto$ExpDay)))) +
# customization
scale_x_continuous(expand = expansion(0)) +
theme_void() +
theme(
plot.margin = unit(rep(.5, 4), "cm"),
strip.text.y = element_text(
vjust = 0.2, size = 12, face = "bold",
margin = margin(r = 15)
),
strip.text.x = element_blank(),
plot.background = element_rect(fill = "white"),
panel.spacing.x = unit(0,"lines")) +
ggtitle(animal, subtitle = "Distance Traveled (m)")
if(export){
fname <- animal
ggsave(paste(rundate,cohort,paste(fname,ftype,sep=""),sep= "_"), width=5,height=6,units="in",path = repo)
}
}