美文网首页绘图绘图技巧做图
ggpubr——绘制各种SCI发表级箱图

ggpubr——绘制各种SCI发表级箱图

作者: R语言与SPSS学习笔记 | 来源:发表于2021-04-13 09:10 被阅读0次

    上次我们给大家介绍了用R的ggpubr包绘制各种散点图,今天我们继续学习ggpubr包绘制各种SCI发表级箱图。

       library(ggpubr)   
    library(patchwork)#如果没有安装要先安装  
     set.seed(123)#设种子数

    首先生成一个随机的数据集,该数据集有300个观测,3个变量(分别是A,sex,smokestatus)

    dataset=data.frame(A=c(rnorm(150,12,2), rnorm(150, 6,1)),
    sex=sample(c("0","1"),300,replace=TRUE),
    smokestatus=sample(c("1","2","3"),300,replace=TRUE))

    绘制简单箱图:(下面的加号表示拼接图形,如果不需要拼接,可以直接把加号去掉)

    ggboxplot(dataset, x = "smokestatus", y = "A",
    bxp.errorbar=T,#显示误差条
    width = 0.5,#箱体的宽度
    color = "smokestatus", #分组
    palette="aaas",#使用杂志aaas的配色
    )+
    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5, color = "smokestatus",
    palette="aaas",bxp.errorbar=T,
    orientation = "horizontal"#调整图形方向为水平
    )+
    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5,color = "smokestatus",
    palette="aaas",bxp.errorbar=T,
    notch = TRUE,#添加缺口
    order = c("3","2","1")#调整顺序
    )+
    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5, color = "smokestatus",
    palette="aaas",bxp.errorbar=T,
    select = c("3")#选择特定的水平来画图 )

    绘制带散点的箱图

    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.8, 
    add = "jitter",#添加图形元素
    add.params=list(color = "smokestatus",size=0.8, shape = 23))#参数add的参数,可设置颜色

    绘制簇状箱图(横坐标表示smokestatus分组,不同颜色代表不同性别sex)

    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.6, 
    color = "black",#轮廓颜色
    fill="sex",#填充
    palette =c("#E7B800", "#00AFBB"),#分组着色
    xlab = F, #不显示x轴的标签
    bxp.errorbar=T,#显示误差条
    bxp.errorbar.width=0.5, #误差条大小
    size=1, #箱型图边线的粗细
    outlier.shape=NA, #不显示outlier
    legend = "right") #图例放右边

    在箱图添加三组总体P值

    ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.8, 
    add = "jitter",add.params=list(color = "smokestatus",size=0.5))+
    stat_compare_means(method = "anova")

    两两比较

    my_comparisons <- list( c("1", "2"), c("1", "3"), c("3", "2") )
    ggboxplot(dataset, x = "smokestatus", y = "A",
    color = "smokestatus", palette = "npg")+
    #两两比较的p值
    stat_compare_means(comparisons = my_comparisons, label.y = c(18, 22, 26))+
    #整体的p值
    stat_compare_means(label.y = 28)

    固定某一组,其他与某组比较

    ggboxplot(dataset, x = "smokestatus", y = "A",
    color = "smokestatus", palette = "npg")+
    # 整体的p值
    stat_compare_means(method = "anova", label.y = 28)+
    stat_compare_means(method = "t.test", #选择统计方法
    ref.group = "1"#以smokestatus为1的作为对照组
    )

    分组/分面之后再做比较

    ggboxplot(dataset, x = "sex", y = "A",
    color = "sex", palette = "npg",
    add = "jitter",
    facet.by = "smokestatus"#按照smokestatus分不同的面板
    )+
    #label中去掉检验方法
    stat_compare_means(aes(label = paste0("p = ", ..p.format..)))

    今天箱图的学习到这。

    欢迎大家关注 R语言与SPSS学习笔记 公众号

    相关文章

      网友评论

        本文标题:ggpubr——绘制各种SCI发表级箱图

        本文链接:https://www.haomeiwen.com/subject/godqlltx.html