<- function(data, title, x, y, outlier.label, xlab, ylab) {
my_ggwithinstats <- rlang::enquo(x)
x <- rlang::enquo(y)
y <- rlang::enquo(outlier.label)
outlier.label
%>%
data ::ggwithinstats(
ggstatsplotx = !!x,
y = !!y,
title = title,
xlab = xlab,
ylab = ylab,
outlier.tagging = TRUE, # whether outliers need to be tagged
outlier.label = !!outlier.label, # variable to be used for tagging outliers
outlier.coef = 2,
pairwise.comparisons = TRUE,
pairwise.display = "significant",
results.subtitle = TRUE,
type = "parametric",
bf.message = FALSE,
p.adjust.method = "none",
point.path = TRUE,
ggtheme = ggprism::theme_prism(),
# package = "RColorBrewer", # "ggsci",
# palette = "Dark", # "default_jco",
violin.args = list(width = 0.9, alpha = 0.2, size = 1, color = "black"),
centrality.point.args = list(size = 5, color = "darkred"),
centrality.label.args = list(size = 3, nudge_x = 0.2, segment.linetype = 5, fill = "#FFF8E7"),
ggplot.component = list(
theme(
plot.title = element_text(hjust = 0, size = 16),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
text = element_text(size = 14)
))+ scale_colour_grey(start = 0.2, end = 0.2) # hacky way to change point color
)
}
# For publication
<- function(data, title, x, y, outlier.label, xlab, ylab,
my_ggwithinstats2 outlier.tagging = FALSE, results.subtitle = TRUE,
centrality.label.args = TRUE, point.path = TRUE,
type = "parametric",
# ... for limits and breaks
...) { <- rlang::enquo(x)
x <- rlang::enquo(y)
y <- rlang::enquo(outlier.label)
outlier.label
if(centrality.label.args){
<- list(size = 3, nudge_x = 0.2, segment.linetype = 5, fill = "#FFF8E7")
centrality.label.args else{
}<- list(size = 0, nudge_x = 10, segment.linetype = 0, alpha = 0) # very hacky way of not showing label
centrality.label.args
}
%>%
data ::ggwithinstats(
ggstatsplotx = !!x,
y = !!y,
title = title,
xlab = xlab,
ylab = ylab,
outlier.tagging = outlier.tagging, # whether outlines need to be tagged
outlier.label = !!outlier.label, # variable to be used for tagging outliers
outlier.coef = 2,
pairwise.comparisons = TRUE,
pairwise.display = "all",
results.subtitle = results.subtitle,
type = type,
bf.message = FALSE,
p.adjust.method = "none",
point.path = point.path,
ggtheme = ggprism::theme_prism(),
# package = "RColorBrewer", # "ggsci",
# palette = "Dark", # "default_jco",
violin.args = list(width = 0.9, alpha = 0.2, size = 1, color = "black"),
centrality.plotting = TRUE,
centrality.type = "parameteric",
centrality.point.args = list(size = 5, color = "darkred"),
centrality.label.args = centrality.label.args,
ggplot.component = list(
theme(
plot.title = element_text(hjust = 0, size = 16),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
text = element_text(size = 14)
))+ scale_colour_grey(start = 0.2, end = 0.2) + # hacky way to change point color
) scale_y_continuous(...)
}
# Fast ggsave - saves plot with filename of R plot object
<- function(plot, device = "png", path = NULL,
fast_ggsave units = "in", dpi = 300, width = 5, height = 5, ...){
<- deparse(substitute(plot))
plot_name ::ggsave(filename = paste0(plot_name, ".", device), plot = plot,
ggplot2device = device, path = path,
units = units, dpi = dpi,
width = width, height = height,
...
)
# use: fast_ggsave(jrad_ox_p, path = savefolder) }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Read, Clean, Recode, Unite
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Read ID data
<- "C:/Users/Mihai/Desktop/R Notebooks/notebooks/PA4-full-report"
folder <- "Scale complete triate Sofi pa4.xlsx"
file setwd(folder)
<- xlsx::read.xlsx2(file.path(folder, file),
id_df startRow = 1, header = FALSE, sheetName = "incadrari")
<- id_df[, 1:8]
id_df colnames(id_df) <- c("Grup", "Cond", "id", "email", "gen", "dir1", "dir2", "dir3")
<-
id_df %>%
id_df ::na_if("") %>%
dplyr::remove_empty("rows") %>%
janitor::mutate(id = stringr::str_remove(id, "^0+"), # remove leading zeros
dplyrid = stringr::str_remove_all(id, "[[:blank:]]"), # remove any white space
id = toupper(id)) %>%
::mutate(Cond = stringr::str_replace(Cond, "12CONTROL", "CONTROL"), # fix typo
dplyrGrup = stringr::str_replace(Grup, "burnout", "Burnout"),
Grup = stringr::str_replace(Grup, "pop generala", "pop gen"),
Grup = stringr::str_replace(Grup, "old", "pop gen")) %>%
::mutate(Grup = dplyr::if_else(is.na(Grup), "pop gen", Grup))
dplyr
<-
id_df %>%
id_df ::separate(id,
tidyrinto = c("id_num", "Exp_type"),
sep = "(?<=[0-9])(?=[A-Za-z])", # ?<= is "look behind"
remove = FALSE
%>%
) ::select(-id_num) %>%
dplyr::mutate(Exp_type = dplyr::if_else(Exp_type %in% c("A", "B", "C", "D", "E", "R", "X"), "online", Exp_type)) %>%
dplyr::mutate(email = tolower(email),
dplyremail = stringr::str_remove_all(email, "[[:blank:]]"))
<-
id_df %>%
id_df ::mutate(across(starts_with("dir"), as.numeric)) %>%
dplyr::filter(id != "9RMN")
dplyr
# check if info in dir colums is correct
<- sort(c(id_df$dir1, id_df$dir2, id_df$dir3)) # 8.9,10 from 1GSR/9RMN
check_ordered <- unique(check_ordered)
check_ordered
<- c(5:228, 230:257) # 229 was skipped
hrv_all
all.equal(check_ordered, hrv_all) # GOOD
# Read HRV data
<- "E:/CINETIC diverse/PA4 HRV"
root_folder <- "temp_PPG"
temp_folder <- file.path(root_folder, "dir1", temp_folder)
dir1_path # dir2_path <- file.path(root_folder, "dir2", temp_folder)
<- file.path(root_folder, "dir3", temp_folder)
dir3_path
<- paste0("IBI_", "dir1", ".csv")
dir1_file # dir2_file <- paste0("IBI_", "dir2", ".csv")
<- paste0("IBI_", "dir3", ".csv")
dir3_file
<- read.csv(file.path(dir1_path, dir1_file))
dir1_df names(dir1_df) <- c("rec", "base", "b_first5", "b_last5")
<-
dir1_df %>%
dir1_df mutate_all(~ifelse(is.nan(.), NA, .)) %>%
mutate(rec = stringr::str_remove(rec, "BUN_M2_Session"),
rec = stringr::str_remove(rec, "_idCD5C_Calibrated_PC.csv"),
rec = as.numeric(rec))
<- read.csv(file.path(dir3_path, dir3_file))
dir3_df names(dir3_df) <- c("rec", "whole", "first5", "last5")
<-
dir3_df %>%
dir3_df mutate_all(~ifelse(is.nan(.), NA, .)) %>%
mutate(rec = stringr::str_remove(rec, "BUN_M2_Session"),
rec = stringr::str_remove(rec, "_idCD5C_Calibrated_PC.csv"),
rec = as.numeric(rec))
# Make merged dataframe
<- dplyr::left_join(id_df, dir1_df, by = c("dir1" = "rec"))
dir_merge <- dplyr::left_join(dir_merge, dir3_df, by = c("dir3" = "rec"))
dir_merge
<-
dir_merge %>%
dir_merge ::drop_na(base, first5, last5)
tidyr
<-
dir_merge_long %>%
dir_merge ::select(-c(b_first5, b_last5, whole)) %>%
dplyr::pivot_longer(cols = c(base, first5, last5), names_to = "Time", values_to = "HRV" ) tidyr
%>%
dir_merge_long ::filter(Cond == "CONTROL") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
xlab = "", ylab = "HRV",
title = "CONTROL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
dir_merge_long ::filter(Cond == "EXPERIMENTAL") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
xlab = "", ylab = "HRV",
title = "EXPERIMENTAL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
dir_merge_long ::filter(Cond == "CONTROL", Grup == "pop gen") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
xlab = "", ylab = "HRV",
title = "CONTROL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
dir_merge_long ::filter(Cond == "EXPERIMENTAL", Grup == "pop gen") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
xlab = "", ylab = "HRV",
title = "EXPERIMENTAL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# dir_merge_long %>%
# dplyr::filter(Cond == "CONTROL", Grup == "PTSD") %>%
# my_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
# xlab = "", ylab = "HRV",
# title = "CONTROL")
%>%
dir_merge_long ::filter(Cond == "EXPERIMENTAL", Grup == "PTSD") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id, type = "np",
xlab = "", ylab = "HRV",
title = "EXPERIMENTAL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
dir_merge_long ::filter(Cond == "CONTROL", Grup == "Burnout") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id,
xlab = "", ylab = "HRV",
title = "CONTROL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
dir_merge_long ::filter(Cond == "EXPERIMENTAL", Grup == "Burnout") %>%
dplyrmy_ggwithinstats2(x = Time, y = HRV, outlier.label = id, type = "np",
xlab = "", ylab = "HRV",
title = "EXPERIMENTAL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=Romanian_Romania.1250 LC_CTYPE=Romanian_Romania.1250 LC_MONETARY=Romanian_Romania.1250 LC_NUMERIC=C
[5] LC_TIME=Romanian_Romania.1250
system code page: 1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggstatsplot_0.8.0 rio_0.5.29 scales_1.1.1 ggpubr_0.4.0 rstatix_0.7.0
[6] broom_0.7.11 PerformanceAnalytics_2.0.4 xts_0.12.1 zoo_1.8-9 psych_2.1.6
[11] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_2.0.1
[16] tidyr_1.1.3 tibble_3.1.4 ggplot2_3.3.5 tidyverse_1.3.1 papaja_0.1.0.9997
[21] pacman_0.5.1
loaded via a namespace (and not attached):
[1] readxl_1.3.1 pairwiseComparisons_3.1.6 backports_1.2.1 plyr_1.8.6 splines_4.1.0
[6] gmp_0.6-2 kSamples_1.2-9 ipmisc_6.0.2 TH.data_1.0-10 digest_0.6.28
[11] SuppDists_1.1-9.5 lmerTest_3.1-3 fansi_0.5.0 magrittr_2.0.1 memoise_2.0.0
[16] xlsx_0.6.5 paletteer_1.4.0 tzdb_0.1.2 openxlsx_4.2.4 modelr_0.1.8
[21] sandwich_3.0-1 colorspace_2.0-2 rvest_1.0.1 ggrepel_0.9.1 haven_2.4.3
[26] xfun_0.25 crayon_1.4.1 jsonlite_1.7.2 lme4_1.1-27.1 zeallot_0.1.0
[31] survival_3.2-13 glue_1.4.2 gtable_0.3.0 emmeans_1.6.3 MatrixModels_0.5-0
[36] statsExpressions_1.1.0 car_3.0-11 Rmpfr_0.8-4 abind_1.4-5 mvtnorm_1.1-2
[41] DBI_1.1.1 PMCMRplus_1.9.0 Rcpp_1.0.7 xtable_1.8-4 performance_0.7.3
[46] tmvnsim_1.0-2 foreign_0.8-81 datawizard_0.2.0.1 httr_1.4.2 ellipsis_0.3.2
[51] farver_2.1.0 pkgconfig_2.0.3 reshape_0.8.8 rJava_1.0-4 multcompView_0.1-8
[56] dbplyr_2.1.1 utf8_1.2.2 janitor_2.1.0 labeling_0.4.2 reshape2_1.4.4
[61] tidyselect_1.1.1 rlang_0.4.11 effectsize_0.4.5 munsell_0.5.0 cellranger_1.1.0
[66] tools_4.1.0 cachem_1.0.6 ggprism_1.0.3 cli_3.0.1 generics_0.1.0
[71] fastmap_1.1.0 BWStest_0.2.2 rematch2_2.1.2 knitr_1.33 fs_1.5.0
[76] zip_2.2.0 WRS2_1.1-3 pbapply_1.4-3 nlme_3.1-152 xml2_1.3.2
[81] correlation_0.7.0 compiler_4.1.0 rstudioapi_0.13 curl_4.3.2 ggsignif_0.6.2
[86] reprex_2.0.1 afex_1.0-1 stringi_1.7.4 parameters_0.14.0 lattice_0.20-44
[91] Matrix_1.3-4 nloptr_1.2.2.2 vctrs_0.3.8 pillar_1.6.3 lifecycle_1.0.1
[96] mc2d_0.1-21 estimability_1.3 data.table_1.14.0 insight_0.14.4 patchwork_1.1.1
[101] R6_2.5.1 BayesFactor_0.9.12-4.2 codetools_0.2-18 boot_1.3-28 MASS_7.3-54
[106] gtools_3.9.2 assertthat_0.2.1 xlsxjars_0.6.1 withr_2.4.2 mnormt_2.0.2
[111] multcomp_1.4-17 bayestestR_0.11.0 parallel_4.1.0 hms_1.1.0 quadprog_1.5-8
[116] grid_4.1.0 minqa_1.2.4 coda_0.19-4 snakecase_0.11.0 carData_3.0-4
[121] numDeriv_2016.8-1.1 lubridate_1.7.10
A work by Claudiu Papasteri