library(tidyverse)#> Warning: package 'purrr' was built under R version 4.5.2#> Warning: package 'stringr' was built under R version 4.5.2#> Warning: package 'forcats' was built under R version 4.5.2#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──#> ✔ dplyr 1.1.4 ✔ readr 2.1.6#> ✔ forcats 1.0.1 ✔ stringr 1.6.0#> ✔ ggplot2 4.0.1 ✔ tibble 3.3.0#> ✔ lubridate 1.9.4 ✔ tidyr 1.3.1#> ✔ purrr 1.2.0 #> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──#> ✖ dplyr::filter() masks stats::filter()#> ✖ dplyr::lag() masks stats::lag()#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errorslibrary(ggpubr)#> Warning: package 'ggpubr' was built under R version 4.5.2library(ggthemes)library(patchwork)#> Warning: package 'patchwork' was built under R version 4.5.2
library(ggplot2)library(rstatix)#> Warning: package 'rstatix' was built under R version 4.5.2#> #> Attaching package: 'rstatix'#> The following object is masked from 'package:stats':#> #> filterlibrary(ggpubr)# sample dataplot_df<-ToothGrowth# Summarize the datasummary_df<-plot_df%>%group_by(dose, supp)%>%summarise( Mean =mean(len), SD =sd(len), SE =sd(len)/sqrt(n())# Standard Error)#> `summarise()` has grouped output by 'dose'. You can override using the#> `.groups` argument.# p values for supp between dosep_sup<-plot_df%>%group_by(supp)%>%t_test(len~dose, p.adjust.method ="fdr")p_sup<-p_sup%>%add_xy_position(x ="dose", group ="supp")# Group the data by dose and then compare the levels of the supps variablep_dose<-plot_df%>%group_by(dose)%>%t_test(len~supp)%>%adjust_pvalue(method ="fdr")%>%add_significance("p.adj")p_dose#> # A tibble: 3 × 11#> dose .y. group1 group2 n1 n2 statistic df p p.adj#> <dbl> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>#> 1 0.5 len OJ VC 10 10 3.17 15.0 0.00636 0.00954#> 2 1 len OJ VC 10 10 4.03 15.4 0.00104 0.00312#> 3 2 len OJ VC 10 10 -0.0461 14.0 0.964 0.964 #> # ℹ 1 more variable: p.adj.signif <chr># Add p-values onto the box plotsp_dose<-p_dose%>%add_xy_position(x ="dose")# change dose to charactersummary_df$dose<-as.character(summary_df$dose)ggplot(summary_df, aes(x =dose, y =Mean, fill =supp))+geom_bar(stat ="identity", color ="black", position =position_dodge())+geom_errorbar(aes(ymin =Mean-SE, ymax =Mean+SE), width =0.2, position =position_dodge(.9))+theme_minimal()+scale_fill_manual( values =c("#999999", "#E69F00"), aesthetics =c("color", "fill"))+labs( title ="ToothGrowth", x ="dose", y ="len ± SE")+theme( plot.title =element_text(hjust =0.5))+stat_pvalue_manual(p_sup, tip.length =0.01, hide.ns =F, color ="supp")+stat_pvalue_manual(p_dose, tip.length =0.01, hide.ns =F, inherit.aes =FALSE)
source("function/calculate_t_tests.R")calculate_t_tests(g,"treatment","liverweight")#> CON LDRT DPVB LR_DPVB#> CON NA NA NA NA#> LDRT 0.8639404014 NA NA NA#> DPVB 0.0193695457 0.0359059495 NA NA#> LR_DPVB 0.0003376202 0.0008265681 0.008820249 NA