美文网首页ggplot2
ggplot2: Boxplot

ggplot2: Boxplot

作者: LET149 | 来源:发表于2023-04-13 09:27 被阅读0次

1. 基础绘图

> bb <- data.frame(name=c(rep("A",3),c("C","D","A","B"),rep("B",3),rep("D",2),rep("C",4)),number=rnorm(16,7,5))
> bb
   name    number
1     A  7.020182
2     A -2.231451
3     A  7.086151
4     C  1.693336
5     D  4.774622
6     A  7.416371
7     B  3.836739
8     B -2.570890
9     B  8.693099
10    B 11.305471
11    D  2.402723
12    D 13.907120
13    C  8.573936
14    C  4.090615
15    C  3.553186
16    C  5.895689

> ggplot(bb, aes(name,number,fill=name))+geom_boxplot()  '#结果为图一;根据name进行填充

> ggplot(bb, aes(name,number,color=name))+geom_boxplot()  #结果为图二;根据name分颜色,只是外框颜色,不进行填充

> ggplot(bb, aes(name,number,color=name))+geom_boxplot()+geom_jitter()  #结果为图三;在画图时将所有数据点也画在图中

> ggplot()+geom_boxplot(data=bb, aes(name, number, color=name))
图一.png
图二.png
图三.png

2. 离群值

ggplot()+geom_boxplot(data=, aes(x=, y=), outlier.size=, outlier.shape=)

    1. outlier.size= : 修改离群值显示形状的大小
    1. outlier.shape= : 修改离群值的形状;NA为不显示离群值;1为空心圆;4为叉号

3. 绘制带有凹槽的箱线图

ggplot()+geom_boxplot(data=, aes(x=, y=), notch=T, notchwidth=0.8, varwidth=T)

    1. notch= : T表示绘制带有凹槽的箱线图
    1. notchwidth= : 控制凹槽处的宽度
    1. varwidth= : T表示箱线图宽度与数据数量成正比
      图片.png

4. 调节一组内各个box之间的距离

参数:position = position_dodge()

ggplot()+geom_boxplot(data = pp,aes(cell_type,mean, fill=gene_type),outlier.shape = NA)+scale_x_discrete(limits=as.factor(cell_type))+ theme_cowplot()+theme(axis.text.x = element_text(angle=-90))+NoLegend()  '#示例一

ggplot()+geom_boxplot(data = pp,aes(cell_type,mean, fill=gene_type),outlier.shape = NA, position = position_dodge(1))+scale_x_discrete(limits=as.factor(cell_type))+theme_cowplot()+theme(axis.text.x = element_text(angle=-90))+NoLegend()  #示例二
示例一
示例二

5. 添加 Error bar

https://zhuanlan.zhihu.com/p/422407418

关键函数:stat_boxplot()

ggplot(data=expressed_used, aes(x=Tissue, y=RPKM_mean, fill=Gene_age))+stat_boxplot(geom = "errorbar", width=0.3, position=position_dodge(0.8))+geom_boxplot(position=position_dodge(0.8))

注意:

    1. geom_boxplot() 放在 stat_boxplot() 后面,则Error bar其中的线会被隐藏在后面
    1. geom_boxplot()stat_boxplot() 中的 position=position_dodge()参数的值要保持一致
Error bar

相关文章

网友评论

    本文标题:ggplot2: Boxplot

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