-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCal_Explore.qmd
171 lines (145 loc) · 4.94 KB
/
Cal_Explore.qmd
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
---
title: "Cal_Explore"
format: html
editor: visual
---
## Load Data
```{r}
#| warning: false
#| echo: false
#| include: false
library(tidyverse)
library(lubridate)
library(ggnewscale)
# Load .Rda from file browser
fpath <- file.choose()
load(fpath)
# Get file name from path
tmp <- unlist(strsplit(fpath,"[\\]"))
filename <- tmp[length(tmp)]
print(paste('Data successfully loaded from',filename))
# Get file directory from path
repo <- paste0(tmp[1:length(tmp)-1],collapse="/")
repo <- paste0(repo,"/")
# Load Cal_Units.csv file
unitkeys <- read_csv(paste0(repo,'Cal_Units.csv'))
```
## Set visualization parameters
```{r}
#| warning: false
#| echo: false
#| include: false
# Specify grouping and faceting variables, color palette
# For color options see http://sape.inf.usi.ch/quick-reference/ggplot2/colour
df2plt <- df.hourly # which binned data you want to vis
var <- "EE" # which variable to plot - use colnames(df2plt)
groupvar <- "Treatment"
facetvar <- NA # set to NA (no quotes!) if no faceting desired
plt <- "Dark2"
by_group <- F # T for mean + SEM, F for individual subjects
# Defaults:
#circadian <- T # get LD shading information automatically
```
#### Create plotting functions
```{r}
tsplot <- function(data,var,groupvar,facetvar,ylab) {
ts <- data
plot <- ggplot() +
# Conditionally facet if facetvar is not NA
{if(!is.na(facetvar) & facetvar %in% colnames(data))facet_grid(~ .data[[facetvar]])} +
# Shaded tiles for photoperiod
geom_tile(data = pp_data,
mapping = aes(x = Time, fill = Photoperiod,y=0),
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" = "gray65",
"Light" = "white",
"Subjective Light" = "gray75"),guide = "none") + new_scale_fill() +
stat_summary(data = ts,
aes(x = Time,
y = .data[[var]],
color = .data[[groupvar]],
group = .data[[groupvar]]),
fun = "mean", geom = "line",
linewidth = 1) +
stat_summary(data = ts,
aes(x = Time,
y = .data[[var]],
color = .data[[groupvar]],
group = .data[[groupvar]],
fill = .data[[groupvar]]),
fun.data = mean_se,
geom = "ribbon",
alpha = 0.5, linetype = 0) +
scale_color_brewer(palette = plt) +
scale_fill_brewer(palette = plt) +
theme_classic() +
# Plot annotations and formatting
labs(x = "Time (hours)", y = ylab) +
scale_x_continuous(expand = expansion(0, 0)) + # no padding on the x-axis
theme_classic() +
theme(text = element_text(size = 12))
}
tsploteach <- function(data,var,groupvar,facetvar,ylab) {
ts <- data
plot <- ggplot() +
geom_tile(data = pp_data,
mapping = aes(x = Time, fill = Photoperiod,y=0),
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" = "gray65",
"Light" = "white",
"Subjective Light" = "gray75"),guide = "none") +
#ggplot(ts, aes(x = Time,
# y = .data[[var]])) +
# Conditionally facet if facetvar is not NA
{if(!is.na(facetvar) & facetvar %in% colnames(data))facet_grid(~ .data[[facetvar]])} +
geom_line(data = ts, aes(x = Time,
y = .data[[var]],
color = Animal),
alpha = 0.5,
linewidth = 1) +
theme_classic() +
# Plot annotations and formatting
labs(x = "Time (hours)", y = ylab) +
scale_x_continuous(expand = expansion(0, 0)) + # no padding on the x-axis
theme_classic() +
theme(text = element_text(size = 12))
return(plot)
}
# Create photoperiod df
pp_data <- df2plt %>%
distinct(Time,Photoperiod,LightCycle) %>%
mutate(Photoperiod = case_when(
LightCycle == "LD" & Photoperiod == "Light" ~ "Light",
LightCycle == "LD" & Photoperiod == "Dark" ~ "Dark",
LightCycle == "DD" & Photoperiod == "Light" ~ "Subjective Light",
LightCycle == "DD" & Photoperiod == "Dark" ~ "Dark"))
```
Generate plots
```{r}
# Get annotations from unitkeys
ylab <- filter(unitkeys,Renamed_Var == {{var}}) %>%
select(Title,Unit) %>%
summarize(ylab = paste(Title,Unit)) %>%
pull()
if (grepl("NA",ylab)) ylab <- title else ylab <- ylab
title <- filter(unitkeys,Renamed_Var == {{var}}) %>%
select(Title) %>%
pull()
if (by_group == T) {
p <- tsplot(df2plt,var,groupvar,facetvar,ylab)
p
} else {
p <- tsploteach(df2plt,var,groupvar,facetvar,ylab)
p
}
```