-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotarodFunctions.R
More file actions
126 lines (98 loc) · 3.45 KB
/
Copy pathRotarodFunctions.R
File metadata and controls
126 lines (98 loc) · 3.45 KB
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
############################ Rotarod FNs #############################################
## groupSummary FN : for one variable
library(dplyr)
library(rstatix)
groupSummary <- function(df, dependVar, type = c("mean_sd", "mean_se", "median_iqr"), ...) {
# Set grouping variables
grouping_vars <- enquos(...)
# Assign type to type
type <- match.arg(type)
# Get summary stats
groupSummary_df <- df %>%
group_by(!!!grouping_vars) %>%
get_summary_stats( {{ dependVar }}, type = type) %>%
mutate(across(where(is.numeric), ~round(.x, digits = 4)))
print(groupSummary_df)
return(groupSummary_df) # Return the result instead of just printing
}
# To Call
# virus_summary <- groupSummary(df, time_to_fall, "mean_se", virus, timepoint)
# View(virus_summary)
# animal_summary <- groupSummary(df, time_to_fall, "mean_se", animal, virus, timepoint)
# View(animal_summary)
## rotarod_lineplot FN
# data - df of choice (summary df)
# y_var - y variable
# y_error - y variable SE
# y_label - y-axis label
# title - graph title
## To make more flexible, add x_var and group_var to FN ---> it is currently set for this project specifically
library(ggplot2)
line_plot <- function(data, y_var, y_error, title, y_label, color_palette = NULL) {
#Default color palette if non provided
if(is.null(color_palette)) {
color_palette <- c('AAV5-EYFP' = '#A3E4D7', 'AAV5-hTyr-1:100' = '#F8C471', 'AAV5-hTyr-Full' = '#E67E22')
}
# Base Plot
p <- ggplot(data, aes(x= timepoint, y = .data[[y_var]], group = virus, color = virus)) +
geom_line(linewidth=1.3) +
geom_errorbar(
aes(ymin = .data[[y_var]] - .data[[y_error]],
ymax = .data[[y_var]] + .data[[y_error]]), #plot error bars
width = 0.2
) +
scale_color_manual(values = color_palette) +
scale_x_discrete(
breaks = c(0,1,2,3,4,10,16)) +
labs(
title = title,
x = "Time Since Injection (weeks)",
y = y_label,
color = "Virus"
) +
theme_minimal() +
theme(
legend.position = 'right',
plot.title = element_text(hjust = 0.5), #center title
panel.border = element_rect(color = 'black', fill = NA, linewidth = 0.8))
print(p)
return(invisible(p))
}
# To Call
# aged <- line_plot(
# data = aged_virus_summary,
# y_var = "mean",
# y_error = "se",
# title = "Rotarod",
# y_label = "Time to Fall (s)")
## lineplot grouped by sex
line_plot2 <- function(data, y_var, y_error, title, y_label, color_palette = NULL) {
#Default color palette if non provided
if(is.null(color_palette)) {
color_palette <- c('AAV5-EYFP' = '#A3E4D7', 'AAV5-hTyr-1:100' = '#F8C471', 'AAV5-hTyr-Full' = '#E67E22')
}
# Base Plot
p <- ggplot(data, aes(x= timepoint, y = .data[[y_var]], linetype = sex, color = virus, group = interaction(virus,sex))) +
geom_line(linewidth = 1.3) +
geom_errorbar(
aes(ymin = .data[[y_var]] - .data[[y_error]],
ymax = .data[[y_var]] + .data[[y_error]]), #plot error bars
width = 0.2
) +
scale_color_manual(values = color_palette) +
scale_x_discrete(
breaks = c(0,1,2,3,4,10,16)) +
labs(
title = title,
x = "Time Since Injection (weeks)",
y = y_label,
color = "Virus"
) +
theme_minimal() +
theme(
legend.position = 'right',
plot.title = element_text(hjust = 0.5), #center title
panel.border = element_rect(color = 'black', fill = NA, linewidth = 0.8))
print(p)
return(invisible(p))
}