简体   繁体   中英

Adjusting the heights of ggpubr derived pvalues manually with facetted plots

Using the following code, I am making facetted plots with pvalues based on comparisons.

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)

在此处输入图片说明

However as you can see, the p values are getting cutoff on the y axis. I would like to be able to reposition them based on the height of the maximum value for a given comparison (ie, the height of the comparison bar and pvalue should adjust automatically according to the height of the highest point within each comparison). Any help or pointers on how to do this would be greatly appreciated!!

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

So after almost 3 years here is your answer to this problem. I had the same issue and literally today as of May 4, 2020 ggpubr version 0.3.0 is released which allows for the inclusion of vjust in stat_compare_means . This allows to move those significance stars or p-values to be moved up or down.

     stat_compare_means(comparisons = my_comparisons, 
                       label = "p.signif", 
                       vjust = 0.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM