【R画图学习10.1】箱图和显著性检验1

这个系列我们学习绘制箱图的绘制技巧,其实这也适用于类似小提琴图等。

library(ggpubr)

library(RColorBrewer)

data("ToothGrowth")

head(ToothGrowth)

先画个基本的箱图:

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE) +

xlab("Dose")+

ylab("Len")

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE) +   #设置notch

xlab("Dose")+

ylab("Len")+

coord_flip()      #旋转箱线图方向

修改异常点的属性,设置outlier的 color, shape and size, outlier.fill:离群点的填充色;outlier.alpha:离群点的透明度

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=4) +

xlab("Dose")+

ylab("Len")

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=4) +

xlab("Dose")+

ylab("Len")+

stat_boxplot(geom = "errorbar",width=0.15)  #添加最大值和最小值的两条须线

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +

xlab("Dose")+

ylab("Len")+

stat_boxplot(geom = "errorbar",width=0.15)+

#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1) #geom_point函数,向箱线图中添加点

geom_jitter(shape=16, size=0.01)  #geom_jitter()函数是geom_point(position = "jitter")的包装


下面进行颜色等的调整。

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +

xlab("Dose")+

ylab("Len")+

stat_boxplot(geom = "errorbar",width=0.15)+

#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)

geom_jitter(shape=16, size=0.01)+  #geom_jitter()函数是geom_point(position = "jitter")的包装

scale_fill_manual(values = c("red","blue","green"))+

theme_classic()+

theme(text = element_text(size = 20))

下面,我们添加不同组之间的统计检验,主要通过stat_compare_means函数实现。

stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE,

label = NULL,  label.x = NULL, label.y = NULL,  ...)

mapping:通过aes()设置绘图时的aesthetic

comparisons:指定一个列表(list),每个列表元素需为长度等于2的向量。向量的内容可以为X轴的两个组别名(字符型),也可以是两个感兴趣组的组别索引(整数值),表示采用指定的两个组别进行比较。

hide.ns:逻辑变量,如果设为TRUE,显示显著性水平时将隐藏 ns 字样,即组间差异不显著时不显示 ns 字样。

label:指定一个字符串,表示标签类型。可为:“p.signif”(显示显著性水平),“p.format”(显示格式化的P值)。

label.x, label.y:指定一个数值,表示显示标签的绝对坐标位置。

:传递给函数compare_means()的参数,如method、paired、ref.group。


my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +

xlab("Dose")+

ylab("Len")+

stat_boxplot(geom = "errorbar",width=0.15)+

#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)

geom_jitter(shape=16, size=0.01)+  #geom_jitter()函数是geom_point(position = "jitter")的包装

scale_fill_manual(values = c("red","blue","green"))+

theme_classic()+

theme(text = element_text(size = 20))+

stat_compare_means(comparisons = my_comparisons)+ ## 增加两两比较的p-value

stat_compare_means(label.y = 50,label.x = 1.5)    # 增加全局p-value

可以调整其它统计检验方法。

ggplot(ToothGrowth, aes(dose, y=len,fill=dose)) +

geom_boxplot(notch=TRUE,outlier.colour="red", outlier.shape=18,outlier.size=2) +

xlab("Dose")+

ylab("Len")+

stat_boxplot(geom = "errorbar",width=0.15)+

#geom_dotplot(binaxis='y', stackdir='center', dotsize=0.1, binwidth = 1)

geom_jitter(shape=16, size=0.01)+  #geom_jitter()函数是geom_point(position = "jitter")的包装

scale_fill_manual(values = c("red","blue","green"))+

theme_classic()+

theme(text = element_text(size = 20))+

stat_compare_means(comparisons = my_comparisons,hide.ns = TRUE,method = "t.test",label = "p.signif")+ ## 增加两两比较的p-value

stat_compare_means(method = "anova",label.y = 50,label.x = 1.5)    # 增加全局p-value

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 箱图geom_boxplot 例子数据格式 namevalueA3.248795062A12.71400397B1...
    曲凉不见阅读 9,017评论 0 7
  • 作者:严涛浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图...
    Dylan的迪阅读 7,703评论 0 6
  • 参考自https://www.cnblogs.com/ljhdo/p/4921588.html 作者:悦光阴 箱线...
    onlyme_862a阅读 14,014评论 0 13
  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 28,503评论 0 67
  • 主要从如何看图、用图与作图三个方面来对箱线图进行理解和总结。 1、看图 如图所示,箱线图是将一组数据按照大小顺序排...
    dowaves阅读 77,503评论 17 109

友情链接更多精彩内容