zahra abdi
zahra abdi

Reputation: 337

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

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.

Upvotes: 1

Views: 40

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26515

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)

Upvotes: 1

Related Questions