s6juncheng / ggpval

Add statistical test or annotation to your ggplot2 plots,
https://s6juncheng.github.io/ggpval/
45 stars 9 forks source link

strange behavior of annotation #17

Open SarahMuse opened 2 years ago

SarahMuse commented 2 years ago
# Create a dataframe
df <- data.frame(A = runif(5), 
                 B = runif(5), 
                 G = c("Group1", "Group2", "Group3", "Group4", "Group5")) 
# Melt the dataframe to be used for ggplot2
df_melt <- reshape2::melt(df, id.vars = "G")

# Create a list of p-values 
# Those values are the real ones that I have in my data
# I just modified them after the calculations by using case_when() with paste() as:
# (1) every p values lower than 0.001 is shown as "p < 0.001"
# (2) all the other values are shown as "p = xxx" 
# As shown on GitHub, annotation option needs a list
pvalues <- list("p < 0.001", "p < 0.001", "'p = 0.123'", "p = 0.813", "p = 0.043")

# Create the plot
library(ggplot2)
library(ggpval)
bar_plot <- ggplot(data = df_melt, aes(x = variable, y = value, fill = variable)) + geom_bar(stat = "identity", position = "dodge") +
  facet_grid(.~G)  +
  theme_bw() +
  scale_y_continuous(labels = scales::percent_format(), limits = c(0, 1.05)) 

# Add p-values
add_pval(bar_plot, pairs = list(c(1, 2)), annotation = pvalues)

Dear ggpval-team,

As you can see, Group 4 and Group 5 looks strange. Not Group 3 as I wrapped the quotations with the character string (I found that by chance as I got a parse issue 2 minutes before that part). My question is: can we avoid that extra step? Or am I doing something wrong?

EDIT: sorry, by also putting the character string for Group 4 and Group 5, it does not work for them...

Thank you for your help

Sarah

System information: R Version: R version 4.1.1 (2021-08-10) R Studio Version: 1.4.1717 OS: Ubuntu 20.04.3 LTS Platform: x86_64-pc-linux-gnu (64-bit)