多组间比较散点图+误差棒(自备)

目录

数据

计算四分位值

作图

数据
rm(list = ls())
library(tidyverse)
library(reshape2)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(cowplot)
#library(Seurat)
data <- iris##鸢尾花数据集
dat <- data[,c(5,1)]#单个数据进行分析
计算四分位值
#根据分组计算四分位及中位数
dat1 <- dat %>% group_by(Species) %>% mutate(upper =  quantile(Sepal.Length, 0.75), 
                                                lower = quantile(Sepal.Length, 0.25),
                                                mean = mean(Sepal.Length),
                                                median = median(Sepal.Length))
> head(dat1)
# A tibble: 6 × 6
# Groups:   Species [1]
  Species Sepal.Length upper lower  mean median
  <fct>          <dbl> <dbl> <dbl> <dbl>  <dbl>
1 setosa           5.1   5.2   4.8  5.01      5
2 setosa           4.9   5.2   4.8  5.01      5
3 setosa           4.7   5.2   4.8  5.01      5
作图
#比较分组
my_comparisons =list( c("setosa","versicolor"),c("versicolor","virginica"),
                      c("setosa","virginica"))
P <- ggplot(dat1,aes(x=Species,y=Sepal.Length)) + #ggplot作图
  geom_jitter(shape = 21,aes(fill=Species),width = 0.25) + 
  stat_summary(fun=mean, geom="point", color="grey60") +
  theme_cowplot() +
  theme(axis.text = element_text(size = 10),axis.title = element_text(size = 10),legend.text = element_text(size = 10),
        legend.title = element_text(size = 10),plot.title = element_text(size = 10,face = 'plain'),legend.position = 'none') + 
  labs(title = "Species",y='Sepal.Lengthage') +
  geom_errorbar(aes(ymin = lower, ymax = upper),col = "grey60",width =  1)+#误差棒
  #差异检验
  stat_compare_means(comparisons=my_comparisons,
                   label.y = c(7.5, 8, 8.5),
                   method="t.test",#wilcox.test
                   label="p.signif")
P
dev.off()

ggplot2画各种误差线和森林图 - 知乎 (zhihu.com)

R进阶绘图--散点箱线图+显著性 / 组间差异比较 / ggpubr包 - 知乎 (zhihu.com)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值