首页 > 解决方案 > 构面中的重要性注释

问题描述

我试图以成对的方式注释下面的图 - 在每个方面比较对应samplesvariable. 本质上是比较CTRposCTRneg等等。我似乎无法让它工作。

这是我的数据和图表:

library(ggpubr)

#data.frame
samples <- rep(c('LA', 'EA', 'CTR'), 300)
variable <- sample(c('pos', 'neg'), 900, replace = T)
stim <- rep(c('rp','il'), 450)
population <- sample(c('EM','CM','TEMRA'), 900, replace = T)
values <- runif(900, min = 0, max = 100)
df <- data.frame(samples, variable, stim, population, values)

#test and comparisons
test_comparisons <- list(c('neg', 'pos'))
test <- compare_means(values ~ variable, data = df, method = 'wilcox.test', 
group.by = c('samples', 'stim', 'population'))

#plot
ggplot(aes(x= variable, y = values, fill = samples), data = df) + 
 geom_boxplot(position = position_dodge(0.85)) +
 geom_dotplot(binaxis='y', stackdir='center', position = 
 position_dodge(0.85), dotsize = 1.5) +
 facet_grid(population ~ stim, scales = 'free_x') +
 stat_compare_means(comparisons = test_comparisons, label = 'p.signif') +
 theme_bw()

阴谋

这只会在 pos 和 neg 之间的每个方面产生 1 个比较,而不是 3...我做错了什么?

标签: rggplot2

解决方案


您可以使用以下代码:

samples <- rep(c('LA', 'EA', 'CTR'), 300)
variable <- sample(c('pos', 'neg'), 900, replace = T)
stim <- rep(c('rp','il'), 450)
population <- sample(c('EM','CM','TEMRA'), 900, replace = T)
values <- runif(900, min = 0, max = 100)
df <- data.frame(samples, variable, stim, population, values)

#test and comparisons
test_comparisons <- list(c('neg', 'pos'))
test <- compare_means(values ~ variable, data = df, method = 'wilcox.test', 
                  group.by = c('samples', 'stim', 'population'))

#plot
ggplot(aes(x= variable, y = values, fill = samples), data = df) + 
geom_boxplot(position = position_dodge(0.85)) +
geom_dotplot(binaxis='y', stackdir='center', position = 
             position_dodge(0.85), dotsize = 1.5) +
facet_grid(population ~ stim+samples, scales = 'free_x') +
stat_compare_means(comparisons = test_comparisons, label = 'p.signif') +
theme_bw()

在此处输入图像描述

希望这能解决您的问题


推荐阅读