简体   繁体   English

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

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

I have a question concerning the plotting of p-values in a violin plot in using ggplot in R. I have a dataframe which contain value ordered by groups : 1000 / 2000 / 3000/ .../n 我有一个关于在R中使用ggplot在小提琴图中绘制p值的问题。我有一个数据框,其中包含按组排序的值:1000/2000/3000 / ... / n

I plot a violin plot from the dataframes (see example below). 我从数据框中绘制了一个小提琴图(请参见下面的示例)。

在此处输入图片说明

My problem is that the last value is equal to length of dataframe. 我的问题是最后一个值等于数据帧的长度。 In some case can be 14470 in another dataframe it can be 16043 or 13789. 在某些情况下,在另一个数据帧中可以是14470,也可以是16043或13789。

I want to plot p-value (wilcoxon test) on my plot by comparing violin 2 by 2. 我想通过将小提琴2与2进行比较来在我的图上绘制p值(wilcoxon检验)。

What I did : 我做了什么 :

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)))

Goal 目标

What I want is to do something shorter than my_comparisons_1000 and which fit the length of my dataframe different dataframes. 我想要做的事情比my_comparisons_1000短,并且适合不同数据帧的数据帧长度。

In this exemple I have groups of 1000 but I also have dataframes with groups of 500. 在此示例中,我有1000个组,但我也有500个组的数据框。

Actually I just have to improve 'my_comparisons_1000' 其实我只需要改善'my_comparisons_1000'

is there a way to generate several vector by step (1000) ? 有没有一种方法可以一步一步生成多个矢量(1000)? Something like rep or seq but I can't find it. 像rep或seq之类的东西,但我找不到。

Something like this? 像这样吗

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)

在此处输入图片说明

For your data, it would be: 对于您的数据,它将是:

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