Home > Blockchain >  R annotate estimates with p < 0.05
R annotate estimates with p < 0.05

Time:11-03

This is an extension of my old question on the topic of error bars. Suppose this is my test data.

df1<-"Group      Est     conf.low      conf.high   pvalue   
       Bi         1.12    0.65          1.603       0.000
       Di        -0.24   -0.44         -0.038       0.02
       Dn        -0.47   -0.80         -0.140       0.005
       HMD       -0.006  -0.32          0.311       0.968
       HMS        -0.72   -1.00         -0.436       0.000
       LM        -0.055  -0.32          0.214       0.6886
       PaS       -1.31   -1.79         -0.850       0.000
       'Ph A'       0.065  -0.250         0.381       0.6885
       TRB        1.023   0.63          1.41        0.000
       TRC       -0.599  -0.94         -0.249       0.0008"
df1 <- read.table(textConnection(df1), header = TRUE)

The script below will generate the error bars without any issues. library(ggplot2)

ggplot(df1, aes(x = Est, y = reorder(Group, -Est)))  
  geom_pointrange(aes(xmin = conf.low, xmax = conf.high), size = 1)  
  geom_text(aes(label = Est), nudge_y = 0.3, size = 4)  
  geom_vline(xintercept = 1, linetype = "dashed", alpha = 0.5)  
  ylab("Group")

My question is how do I add a * next to the estimate values on error bar, only those with p < 0.05

Expecting a plot like this.

enter image description here

I can do this manually using the annotate function but I am interested in a solution that is more automated and not having to add many lines of annotation. Thanks in advance.

CodePudding user response:

You may try

df2 <- df1 %>%
  filter(pvalue < 0.05) 
ggplot(df1, aes(x = Est, y = reorder(Group, -Est)))  
  geom_pointrange(aes(xmin = conf.low, xmax = conf.high), size = 1)  
  geom_text(aes(label = Est), nudge_y = 0.3, size = 4)  
  geom_vline(xintercept = 1, linetype = "dashed", alpha = 0.5)  
  ylab("Group")  
  geom_text(data = df2, aes(x = Est, y = reorder(Group, -Est), label = "*"), size = 5, nudge_y = .3, nudge_x = .2, color = "red")

enter image description here

  • Related