R语言 分组箱形图上的Wilcox检验和p值

b5lpy0ml  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(155)

我想计算统计数据并绘制ROI(Reg1和Reg2之间、Reg1和Reg3之间、Reg2和Reg3之间)中每个Cell_type(20_p1、4_p1等)的显著性p值。

structure(list(Value = c(4.621072089, 12.59398496, 0, 0, 10.16507385, 
23.37278107, 8.474576271, 0, 7.397959184, 11.02803738, 1.694915254, 
4.347826087, 9.6024006, 27.98053528, 8.450704225, 0, 8.78112713, 
19.33471933, 0, 0, 20.44534413, 27.79809802, 20.51282051, 50, 
28.00150546, 45.01811594, 26.47058824, 50, 0, 0.113765643, 0.266666667, 
0, 0.520833333, 0.393700787, 0.595238095, 0, 0.875912409, 0.965517241, 
1.731601732, 0), Cell_type = c("20_p1", "4_p1", "8_p1", "68_p1", 
"20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", 
"20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", 
"20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", 
"20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", 
"20_p1", "4_p1", "8_p1", "68_p1"), Area = c("GC1", "GC1", "GC1", 
"GC1", "GC2", "GC2", "GC2", "GC2", "GC3", "GC3", "GC3", "GC3", 
"GC4", "GC4", "GC4", "GC4", "GC5", "GC5", "GC5", "GC5", "GC_sc1", 
"GC_sc1", "GC_sc1", "GC_sc1", "GC_sc2", "GC_sc2", "GC_sc2", "GC_sc2", 
"Foll1", "Foll1", "Foll1", "Foll1", "Foll2", "Foll2", "Foll2", 
"Foll2", "Foll3", "Foll3", "Foll3", "Foll3"), ROI = c("Reg1", 
"Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", 
"Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", 
"Reg1", "Reg1", "Reg1", "Reg2", "Reg2", "Reg2", "Reg2", "Reg2", 
"Reg2", "Reg2", "Reg2", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", 
"Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3"), Site = c("Normal", 
"Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", 
"Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", 
"Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", 
"Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", 
"Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", 
"Normal", "Normal", "Normal", "Normal")), class = "data.frame", row.names = c(NA, 
-40L))

我使用的代码如下:

p= ggplot(Data, aes(x=ROI, y=Value, fill=ROI)) +
geom_boxplot() +geom_point()+
theme_classic()+ylim(0,100) +
facet_grid(cols=vars(Cell_type), rows=vars(Site)) +
rotate_x_text(angle = 45) 
df_wilcox <- Data %>%
group_by(Cell_type, ROI) %>%
pairwise_wilcox_test(Value ~ ROI) %>%
add_y_position(step.increase = 0.02)

它给出错误:"" mutate()中的错误:!计算data = map(.data$data, .f, ...)时出现问题。由pull()中的错误导致:!无法提取不存在的列。列ROI不存在。

y3bcpkx1

y3bcpkx11#

这里有两个问题,一个是您已经按照ROICell_type对 Dataframe 进行了分组,但是pairwise_wilcox_test需要比较每个分组中不同的ROI级别,因此您必须只按照Cell_type进行分组。
我遇到的第二个问题与您的样本数据有关,因为68_p1中的两个组的方差为零,这似乎会导致计算错误,因此我不得不过滤掉这种单元格类型:

df_wilcox <- Data %>%
  filter(Cell_type != '68_p1') %>%
  group_by(Cell_type) %>%
  pairwise_wilcox_test(., Value ~ ROI) %>%
  add_xy_position()

p + stat_pvalue_manual(df_wilcox, label = "p.adj.signif", inherit.aes = FALSE)

    • 编辑**

ggpubr框架中完成整个过程可能是最简单的:

ggboxplot(Data, 'ROI', 'Value', facet.by = 'Cell_type',
          fill = 'ROI') +
  geom_point(position = position_jitter(0.3)) +
  stat_compare_means(comparisons = list(c('Reg1', 'Reg2'),
                                        c('Reg1', 'Reg3'),
                                        c('Reg2', 'Reg3'))) +
  stat_compare_means(label.y = 90) +
  ylim(c(0, 100)) +
  facet_grid(Site~Cell_type) +
  theme_classic()

相关问题