ggpubr绘制箱线图

作者: 灵活胖子的进步之路 | 来源:发表于2020-12-14 15:52 被阅读0次

    首先线看下用法

    ggboxplot(data, x, y, combine = FALSE,merge=FALSE, color ="black", fill ="white", palette =NULL, title =NULL, xlab =NULL, ylab =NULL, facet.by =NULL, panel.labs =NULL, short.panel.labs =TRUE, linetype ="solid",size=NULL, width =0.7, notch =FALSE,select=NULL, remove =NULL,order=NULL,add="none", add.params =list(),error.plot ="pointrange", label =NULL, font.label =list(size=11, color ="black"), label.select =NULL, repel =FALSE, label.rectangle =FALSE,ggtheme = theme_pubr(), ...)

    以下为各变量解释

    data所需的数据框 dataframex
    y进行作图所需的数据
    combine对于多个变量的数据是否分面,默认是FALSE
    merge对于多个变量的数据是否合并,默认是FALSE
    color轮廓线的颜色
    fill填充色
    palette自定义颜色画板
    title设置标题
    xlab设置x轴标题
    ylab设置y轴标题
    orientation变换坐标轴的方向
    facet.by设置分组分面
    panel.labs设置分面各组的标题
    short.panel.labs是否缩写分面标题,逻辑值,默认是TRUE
    linetype线的类型
    size设置点和轮廓线的大小
    width设置柱子的宽度,取值范围 0~1
    notch否添加缺口
    select选择需要展示的变量
    remove移除不需要展示的变量
    order选定变量的排列顺序
    add添加图片元素:"none", "dotplot", "jitter", "boxplot", "point", "mean", "mean_se", "mean_sd", "mean_ci", "mean_range", "median", "median_iqr", "median_mad", "median_range"
    add.params给add参数中添加的元素添加属性:olor, shape, size, fill, linetypeeg:add.params = list(color = "red")error.plot添加误差棒,选项有"pointrange", "linerange", "crossbar", "errorbar", "upper_errorbar", "lower_errorbar", "upper_pointrange", "lower_pointrange", "upper_linerange", "lower_linerange"。默认是"pointrange" or "errorbar"
    label设置列标签font.label设置标签字号(e.g.: 14)、类型(e.g.: "plain", "bold", "italic", "bold.italic")、颜色(e.g.: "red")如:font.label = list(size = 14, face = "bold", color ="red")
    repel逻辑值,是否使用ggrepel避免字体重叠
    label.rectangle是否给标签添加方框ggtheme设置画图主题,默认是theme_pubr()。ggplot2 official themes: theme_gray(), theme_bw(), theme_minimal(), theme_classic(), theme_void()

    以下实战,本次以survival包里面的colon数据集为例

    library(ggpubr)
    library(survival)
    data(colon)
    df <- colon
    attach(df)
    str(df)
    
    colon数据集结构
    ggboxplot(df, x = "rx", y = "age", width = 0.8)#基础模式
    
    基础模式,不同化疗方案的年龄分布情况
    # 变换坐标轴的方向
    ggboxplot(df, x = "rx", y = "age", orientation = "horizontal")
    
    变化坐标轴方向
    # 添加箱线图缺口
    ggboxplot(df, x = "rx", y = "age", notch = TRUE)
    
    添加缺口
    # 添加 jitter points 扰动点并根据不同的分组赋予不同形状
    ggboxplot(df, x = "rx", y = "age", add = "jitter", shape = "rx")
    
    扰动点并根据不同的分组赋予不同形状
    # 可以选择要展示的特定列
    ggboxplot(df, x = "rx", y = "age", select = c("Obs", "Lev"))
    
    展示特定列
    # 指定排列顺序
    ggboxplot(df, x = "rx", y = "age", order = c("Lev+5FU","Obs", "Lev"))
    
    
    指定横坐标顺序
    # 更改填充色和轮廓线的颜色
    ggboxplot(df, "rx", "age", color = "black", fill = "gray")
    
    改变颜色
    #设置色板并按照x分组填充
    ggboxplot(df, "rx", "age", 
              fill = "rx", 
              palette = c("#00AFBB", "#E7B800", "#FC4E07"))
    
    设置色板并按照x分组填充
    #引入另外一个分组变量"sex",并根据"sex"分组定义颜色,色板选择了JCO
    ggboxplot(df, "rx", "age", 
              color = "sex",
              palette = "jco")
    
    引出变量SEX,相当于对其进行亚组分析
    # 根据"sex"分组进行分组分面
    ggboxplot(df, "rx", "age", 
              color = "sex",
              facet.by = "sex",
              palette = "jco")
    
    根据"sex"分组进行分组分面
    # 根据“rx”分组匹配不同的点的形状
    ggboxplot(df, "rx", "age", 
              color = "rx",
              add = "jitter", 
              shape="rx",
              palette = "jco")
    
    根据“rx”分组匹配不同的点的形状
    ggboxplot(df, "rx", "age", 
              color = "rx",#分组颜色
              title="不同化疗方案的年龄分布情况",
              xlab="化疗方案分组",
              ylab="年龄",
              facet.by="sex",
              panel.labs=list(sex=c("男性","女性")),
              notch=T,
              palette = "jco")
    
    最终图形

    相关文章

      网友评论

        本文标题:ggpubr绘制箱线图

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