Search code examples
rggplot2plotcolorsglm

Adding odds ratios values and different colors in a ggplot graph in R


I am trying to insert the Odds ratio values in the graph. I'm also trying to give different color to the shapes with significant Odds ratio. Significant Odds ratio are those whose confidence intervals does not include a value of 1. Can someone, please, help me with this?

My code exemple is:

df <- data.frame(yAxis = length(boxLabels):1,
boxOdds = c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
boxCILow = c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
boxCIHigh = c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584))

Ploting the graph

ggplot(df, aes(x = boxOdds, y = boxLabels)) +
  geom_vline(aes(xintercept = 1), size = .25, linetype = 'dashed') +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = 
      .2, color = 'gray50') +
  geom_point(size = 3.5, color = 'orange') +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_x_continuous(breaks = seq(0,7,1) ) +
  coord_trans(x = 'log10') +
  ylab('') +
  xlab('Odds ratio (log scale)') +
  annotate(geom = 'text', y =1.1, x = 3.5, label ='Model p < 0.001\nPseudo 
R^2 = 0.10', size = 3.5, hjust = 0) + ggtitle('My title')

Solution

  • I made a dummy variable for boxLabels since your code was missing it.

    One thing to note for your future plots : define everything you need for the plot beforehand. Such as the a variable detailing whether or not your test is signifcant.

    boxLabels <- letters[1:8] # dummy boxlabels variable
    
    df <- data.frame(yAxis = length(boxLabels):1,
        boxOdds = c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
        boxCILow = c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
        boxCIHigh = c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584),
        boxLabels = boxLabels # Don't forget to actually put it in the dataframe if you use it in the plot.
    ) 
    
    # Make a variable for whether your test is significant
    df$isSignif <- df$boxCILow > 1 | df$boxCIHigh < 1
    
    g <- ggplot(df, aes(x = boxOdds, y = boxLabels)) +
      geom_vline(aes(xintercept = 1), size = .25, linetype = 'dashed') +
      geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = 
          .2, color = 'gray50') +
      geom_point(aes(color = isSignif), size = 3.5) + ## Use the variable to define the color of your plot.
      theme_bw() +
      theme(panel.grid.minor = element_blank()) +
      scale_x_continuous(breaks = seq(0,7,1) ) +
      coord_trans(x = 'log10') +
      ylab('') +
      xlab('Odds ratio (log scale)') +
      annotate(geom = 'text', y =1.1, x = 3.5, label ='Model p < 0.001\nPseudo 
    R^2 = 0.10', size = 3.5, hjust = 0) + ggtitle('My title')
    
    # add the oddsRatio values to the plot
    g <- g + geom_text(label=round(df$boxOdds, 2), nudge_y=0.25)
    g
    

    The only lines I changed from your code are those with a comment.

    This outputs the following plot :

    enter image description here