使用以下代码,我将根据比较制作带有p值的刻面图。
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轴上被截止。我希望能够根据给定比较的最大值的高度重新定位它们(即,比较条和p值的高度应根据每个比较中最高点的高度自动调整)。任何帮助或指示如何做到这一点将非常感谢!!
答案 0 :(得分:0)
我找到的解决方案是增加高度与宽度的比率。
答案 1 :(得分:0)
因此,将近3年后,您将可以解决此问题。我遇到了同样的问题,实际上从2020年5月4日起,发布了ggpubr
版本0.3.0
,该版本允许在vjust
中包含stat_compare_means
。这样可以将重要的恒星或p值向上或向下移动。
stat_compare_means(comparisons = my_comparisons,
label = "p.signif",
vjust = 0.5)