繁体   English   中英

R -ggplot-在图形上绘制p值

[英]R -ggplot - Plot p-value on figure

我有一个关于在R中使用ggplot在小提琴图中绘制p值的问题。我有一个数据框,其中包含按组排序的值:1000/2000/3000 / ... / n

我从数据框中绘制了一个小提琴图(请参见下面的示例)。

在此处输入图片说明

我的问题是最后一个值等于数据帧的长度。 在某些情况下,在另一个数据帧中可以是14470,也可以是16043或13789。

我想通过将小提琴2与2进行比较来在我的图上绘制p值(wilcoxon检验)。

我做了什么 :

my_comparisons_1000 <- list \
(c("1000", "2000"),c("2000", "3000"),\
c("3000", "4000"),c("4000","5000"),\
c("5000","6000"),c("6000","7000"),\
c("7000","8000"),c("8000","9000"),\
c("9000","10000"),c("10000","11000"),\
c("11000","12000"),c("12000","13000"),\
c("13000","14000"))

fig_1000<-ggplot(violin, aes(x=range_1000, y=mean_region))+
    geom_violin(scale = "width",adjust = .5,fill='#A4A4A4', color="darkred")+
    geom_boxplot(width=0.1,outlier.shape = NA) + theme_minimal()+
    scale_x_discrete(labels=c(seq(1000,length(violin[,1]),by=1000), length(violin[,1])))+
    stat_summary(fun.y=mean, geom="point",size=1,color="red",aes(shape="Mean")) +

    stat_compare_means(comparisons = my_comparisons_1000,label.y = 14)+ # Add pairwise comparisons p-value

    theme(axis.text.x = element_text(angle = 90, hjust = 1))+
    guides(colour=guide_legend(order=1), shape=guide_legend(title=NULL, order=2)))

目标

我想要做的事情比my_comparisons_1000短,并且适合不同数据帧的数据帧长度。

在此示例中,我有1000个组,但我也有500个组的数据框。

其实我只需要改善'my_comparisons_1000'

有没有一种方法可以一步一步生成多个矢量(1000)? 像rep或seq之类的东西,但我找不到。

像这样吗

library(tidyverse)
library(ggpubr)
tidyiris <- iris %>% gather(key, value, -Species)
num_pairs <- length(unique(tidyiris$key)) - 1
my_comparisons <- map(seq(1, num_pairs, 1), ~c(.x, .x+1))
ggplot(tidyiris, aes(key, value)) + geom_violin() + 
  stat_compare_means(comparisons = my_comparisons)

在此处输入图片说明

对于您的数据,它将是:

my_comparisons <- map(seq(1000, violin$range_1000 - 1000, 1000), ~c(.x, .x + 1000))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM