How to replace the na with nothing in ggplot2 but not the other significance level?

Viewed 23

I just want to have star(s) in my plot and not the na. How to replace the na with nothing in ggplot2 but not the other significance level?

Here is the script:

ggplot(df, aes(x=Gene, y=Count, fill=Stage))+
  geom_boxplot()+theme_bw()+
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black'))+
  stat_compare_means(label.y = 15.5,label = "p.signif")

enter image description here

Thanks for any help.

1 Answers

It's difficult to answer your question without a minimal, reproducible example but here is one potential solution:

library(ggplot2)
library(ggpubr)

# use an example dataset
df <- ToothGrowth

# Default settings (has "ns")
ggplot(df, aes(x=supp, y=len, fill=dose))+
  geom_boxplot()+
  theme_bw()+
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black'))+
  stat_compare_means(label = "p.signif")


# Only plot the p.signif if it's <0.05
ggplot(df, aes(x=supp, y=len, fill=dose))+
  geom_boxplot()+
  theme_bw()+
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black'))+
  stat_compare_means(aes(label = ifelse(..p.signif.. <= 0.05, ..p.signif.., "")))

Created on 2022-08-23 by the reprex package (v2.0.1)

Related