我有一个关于在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))