1

使用以下代码,我根据比较使用 pvalues 制作多面图。

library(ggpubr)
library(ggplot2)
library(dplyr)
library(stringr)
library(reshape2)
library(ggsci)

df <- melt(iris)
mycomparisons <- list(c("setosa", "versicolor"), c("setosa", "virginica"))

# Calculate the maximum value for each facetted plot to scale the axis limits
dmax = df  %>% group_by(variable) %>% 
  filter(value==max(value, na.rm=TRUE))

# Determine the number of replicates to use for modifying a dummy dataframe for scaling the y axis limits
num_reps <- df %>% group_by(Species,variable) %>% tally()
times <- sum(num_reps$n[c(1:length(unique(df$Species)))])

# Create a dummy dataframe to use for manually adjusting the y axis limits
dumdum <- df
dumdum$value <- c(rep(0, times = 0.5*times), rep(dmax$value[1]*1.2, times = 0.5*times),
                  rep(0, times = 0.5*times), rep(dmax$value[2]*1.2, times = 0.5*times),
                  rep(0, times = 0.5*times), rep(dmax$value[3]*1.2, times = 0.5*times),
                  rep(0, times = 0.5*times), rep(dmax$value[4]*1.2, times = 0.5*times))

ggplot(data = df, 
       aes(x = Species, y = value, color = Species)) +
  geom_boxplot(outlier.shape = NULL) +
  geom_point() +
  xlab("") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) +
  scale_color_jco() +
  facet_wrap(facets = "variable", scales = 'free_y') +
  geom_blank(data = dumdum) +
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     aspect.ratio = 1, text = element_text(family="Helvetica")) +
  stat_compare_means(paired = FALSE, method = 't.test', label = "p.signif",
                     show.legend = FALSE, comparisons = mycomparisons)

在此处输入图像描述

但是,如您所见,p 值在 y 轴上被截断。我希望能够根据给定比较的最大值的高度重新定位它们(​​即,比较条的高度和 pvalue 应该根据每次比较中最高点的高度自动调整)。任何有关如何执行此操作的帮助或指示将不胜感激!

4

2 回答 2

0

我找到的解决方案是增加高度与宽度的比率。

于 2018-04-04T07:28:14.933 回答
0

因此,将近 3 年后,这里是您对这个问题的答案。我遇到了同样的问题,从字面上看,从 2020 年 5 月 4 日开始,今天发布的ggpubr版本0.3.0允许包含vjuststat_compare_means. 这允许向上或向下移动这些重要性星或 p 值。

     stat_compare_means(comparisons = my_comparisons, 
                       label = "p.signif", 
                       vjust = 0.5)
于 2020-05-04T13:47:24.270 回答