R语言ggstatsplot包做T检验

R语言用ggstatsplot包做方差分析和绘图

R语言ggstatsplot包做卡方检验

library(ggstatsplot)
library(dplyr)

mtcars_new <- mtcars %>% 
  tibble::rownames_to_column(., var = 'carname') #将mtcars的行名转换成'carname列存储,形成新的数据集
ggdotplotstats(mtcars_new, x = mpg, y = carname, 
               centrality.para = F, #不显示集中趋势统计量
               results.subtitle = F,  #不在图中以副标题的形式显示统计结果
               ggtheme = ggplot2::theme_classic(),#设置主题
               messages = F
               )
image.png

单样本均值比较

1、点图
ggdotplotstats(mtcars_new, x = mpg, y = carname, 
               centrality.para = 'mean', #集中趋势选择均数(可选mean和median)
               test.value = 15, #样本均数与15进行比较
               test.value.line = T, #画出比较值的垂直线
               test.value.color = 'red', #比较值的标签颜色为red
               test.value.size = 1.2#垂直线的宽度为1.2倍
                ) 
Note: Shapiro-Wilk Normality Test for mpg : p-value = 0.123
image.png
2、频数图
gghistostats(mtcars_new, x = mpg, 
             binwidth = 3, #组距为3
             normal.curve = T, 
             normal.curve.color = 'Orange',
             centrality.para = 'mean',
             test.value = 15, 
             test.value.line = T, 
             test.value.color = 'red',
             bar.measure = 'mix' #既显示频数又显示频率
             )
Note: Shapiro-Wilk Normality Test for mpg : p-value = 0.123
image.png

点图与频数图若不设置“test.value”,则默认与0进行比较。

两样本均值比较

str(sleep)
'data.frame':   20 obs. of  3 variables:
 $ extra: num  0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
 $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
 $ ID   : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
1、两独立样本
ggbetweenstats(sleep, x = group, y = extra, 
               type = 'p', #参数(parameter)检验, np为非参数检验
               conf.level = 0.95,
               mean.ci = T #图中显示均值的置信区间
                )

Note: Shapiro-Wilk Normality Test for extra : p-value = 0.311
Note: Bartlett's test for homogeneity of variances for factor group: p-value = 0.743
image.png
2、两配对样本
ggwithinstats(sleep, x = group, y = extra, 
              type = 'p',
              conf.level = 0.95,
              mean.ci = T)

Note: Shapiro-Wilk Normality Test for extra : p-value = 0.311
Note: Bartlett's test for homogeneity of variances for factor group: p-value = 0.743
image.png

你可能感兴趣的:(R语言ggstatsplot包做T检验)