R优雅的进行统计分析(1) T_test

2022-09-21 14:40:21 浏览数 (1)

❝本节来介绍如何使用R语言来做数据统计分析,通过「rstati」包进行t-test,完全使用tidyverse体系进行数据清洗及可视化 ❞

安装并加载R包

代码语言:javascript复制
package.list=c("tidyverse","rstatix","ggtext")

for (package in package.list) {
  if (!require(package,character.only=T, quietly=T)) {
    install.packages(package)
    library(package, character.only=T)
  }
}

数据清洗

❝自定义计算sd,se;以「0.5」为对照进行t_test,通过dplyr整理绘图数据,去掉NA,ns

代码语言:javascript复制
df <- ToothGrowth %>%
  mutate(dose=as.factor(dose)) %>% 
  group_by(dose) %>%
  summarise(value_mean=mean(len),sd=sd(len),se=sd(len)/sqrt(n())) %>% 
  left_join(.,ToothGrowth %>% t_test(data =., len ~ dose, ref.group  =  "0.5") %>% 
              adjust_pvalue(method = "bonferroni") %>% 
              select(group2,p.adj.signif),by=c("dose"="group2")) %>% 
  mutate(p.adj.signif = replace_na(p.adj.signif,""),across("p.adj.signif",str_replace,"ns","")) %>% 
  ungroup()

数据可视化

❝使用 scale_y_continuous(expand=c(0,0))后会导致添加文本显示不全,此处通过创建两个文本几何对象来加大Y轴范围 ❞

代码语言:javascript复制
ggplot(df,aes(dose,value_mean,fill=dose)) 
  geom_errorbar(aes(ymax = value_mean   sd, ymin = value_mean - sd),width = 0.1,color = "grey30") 
  geom_col(width=0.4) 
  geom_text(aes(label=p.adj.signif, y = value_mean   sd  1.5), size = 3, color = "white",
            show.legend = FALSE) 
  geom_text(aes(label=p.adj.signif, y = value_mean   sd   0.2),size=5, color = "black",
            show.legend = FALSE) 
  scale_y_continuous(expand=c(0,0))  
  theme_minimal()  
  theme(axis.title.x = element_blank(),
        axis.line = element_line(color = "#3D4852"),
        axis.ticks = element_line(color = "#3D4852"),
        panel.grid.major.y = element_line(color = "#DAE1E7"),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(rep(0.2,4),"cm"),
        axis.text = element_text(size = 12, color = "#22292F"),
        axis.title = element_text(size = 12, hjust = 1),
        axis.title.y = element_text(margin = margin(r = 12)),
        axis.text.y = element_text(margin = margin(r = 5)),
        axis.text.x = element_text(margin = margin(t = 5)),
        legend.position = "non") 
  scale_fill_brewer(palette="Blues")

0 人点赞