构面中的重要性注释

时间:2018-10-29 11:52:27

标签: r ggplot2

我正在尝试以成对方式注释以下图-在每个方面比较samples中对应的variable。本质上比较CTR中的posCTR中的neg,依此类推。我似乎无法使其正常工作。

这是我的数据和图表:

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

plot

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

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

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

enter image description here

希望这可以纠正您的问题