其他
R优雅的进行统计分析(1) T_test
欢迎关注R语言数据分析指南
❝本节来介绍如何使用R语言来做数据统计分析,通过「rstati」包进行
❞t-test
,完全使用tidyverse
体系进行数据清洗及可视化
安装并加载R包
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
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轴范围
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")
❝本节介绍到此结束,很小的一个案例但是应该很实用,喜欢的观众老爷们欢迎点赞转发,如果需要获取之前绘图文档数据及代码的欢迎加入小编的
❞2022年VIP交流群,付费99元
,群内会同步上传公众号文档代码;目前已上传2021-2022约150篇文档代码
;添加小编微信时,请备注单位-方向-姓名以及来意以便高效处理
免费交流群
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流