最终出图如下
这里自动做统计检验的函数是 stat_compare_means()
读入数据
作图
这个函数来自于ggpubr这个包,只需要指定根据那一列来分组就可以了
默认的是Wilcoxon Rank Sum and Signed Rank Tests,如果要用t检验指定method参数
如果想把P值改成星号,直接加label=“p.signif”参数
这里如果不显著会在图上显示ns,如果不想要ns,可以加 hide.ns = TRUE 参数
星号的位置可以手动指定,用 label.y = c(26,31) 参数
使用到的是 ggsignif 这个包
小明的数据分析笔记本
这里,我们采用了stripchart函数以及RColorBrewer包来添加数据点并调整颜色。
注意,此时应当利用boxplot函数里面的内置参数names设置每个箱线图的特征名。并且将stripchart函数里面的数据用列表并起来。
这里,我们使用了dplyr包,将数据按照不同类别(1,2,3,4)来分成新的数据,并且,在boxplot函数里,不同于上述几种方式,其数据的给定是以公式的方式给出,形如“特征~类别,数据名”的样式。
>require(car)>attach(women)
>#car这个包的全称是Companion to Applied Regression
>scatterplot(weight~height,lty.smooth=2,pch=19,col="red",main="Women Age 30-39",xlab="Height in Inches",ylab="Weight in Pounds")
Warning messages:
1: In plot.window(...) : "lty.smooth" is not a graphical parameter
2: In plot.xy(xy, type, ...) : "lty.smooth" is not a graphical parameter
3: In axis(side = side, at = at, labels = labels, ...) :
"lty.smooth" is not a graphical parameter
4: In axis(side = side, at = at, labels = labels, ...) :
"lty.smooth" is not a graphical parameter
5: In box(...) : "lty.smooth" is not a graphical parameter
6: In title(...) : "lty.smooth" is not a graphical parameter
>#car这个包中的scatterplot命令的主要优势在于除了有传统的散点图之外
>#还能包含平滑拟合曲线
>#最主要的是还能够加上x与y数据的箱线图
>
>
>#用到的包是car
>#用到的数据集是women(主要是身高与体重的数据)
>#用到的命令是scatterplot()