1

I'm trying to make a publication ready plot with odds ratios as well as upper und lower ci's. Now I did get nice graphs with ggplot but I want to add significance stars above the geom_point-layer, that correspond to the three levels (.005,.001,.001). I made up an example dataframe which I'm getting my values from. It also includes the p-values:

or<-as.numeric(round(rnorm(5,0,1),2))

n<-c(100,100,100,100,100)

p<-c(.06,.05,.01,.0012,.000034)

df<-as.data.frame(cbind(or,n,p))

df$names<-as.factor(c(1:5))

df$lwr.ci<-as.numeric(or-2)

df$upr.ci<-as.numeric(or+2)

The ggplot-code is as follows:

p <- ggplot(df, aes(or, fct_rev(names)))+
  theme_bw(base_family = "Times New Roman")


p+geom_errorbarh(aes(xmax =upr.ci, xmin = lwr.ci), size = 1, height = 0, color = 'gray') +
  geom_point(size = 2, color = 'black')  +
  theme(panel.grid = element_blank()) +
  scale_x_continuous(breaks = c(0:13),limits = c(-4,5))+
  ylab('CD') +
  xlab('Odds Ratios')+
  ggtitle('R')+
  geom_text(parse=T,aes(label=paste(round(or,2),sep =" ","(","italic(n)==",n,")")),x= -3.5, size = 3,color='black',family="Times New Roman")

If anybody has a suggestions on how to add the corresponding significance stars to the geom_points, I would be very grateful.

Jan
  • 61
  • 1
  • 8
  • Maybe this https://stackoverflow.com/questions/9723239/ggplot2-annotation-with-superscripts could help. – holzben Nov 24 '20 at 15:11

1 Answers1

1

Try creating the stars in a new variable like this, first the data:

library(dplyr)
library(ggplot2)
#Data
or<-as.numeric(round(rnorm(5,0,1),2))

n<-c(100,100,100,100,100)

p<-c(.06,.05,.01,.0012,.000034)

df<-as.data.frame(cbind(or,n,p))

df$names<-as.factor(c(1:5))

df$lwr.ci<-as.numeric(or-2)

df$upr.ci<-as.numeric(or+2)

Now, the code for labels:

#Labels
plab <- ifelse(p<0.001,'***',ifelse(p<0.01,'**',ifelse(p<0.05,'*','')))
df$plab <- plab
#Plot
p <- ggplot(df, aes(or, fct_rev(names)))
#Plot 2
p+geom_errorbarh(aes(xmax =upr.ci, xmin = lwr.ci), size = 1, height = 0, color = 'gray') +
  geom_point(size = 2, color = 'black')  +
  theme(panel.grid = element_blank()) +
  scale_x_continuous(breaks = c(0:13),limits = c(-4,5))+
  ylab('CD') +
  xlab('Odds Ratios')+
  ggtitle('R')+
  geom_text(parse=T,aes(label=paste(round(or,2),
                                    sep =" ","(","italic(n)==",n,")")),
            x= -3.5, size = 3,color='black',family="Times New Roman")+
  geom_text(aes(label=plab),vjust=-0.5,fontface='bold')

Output:

enter image description here

Let me know if any adjustment is necessary.

Duck
  • 39,058
  • 13
  • 42
  • 84