Search code examples
rggplot2plotconfidence-interval

Is it possible to use sjplot::plot_model() to plot confidence interval along y-axis?


I would like to plot vertical confidence interval graph instead of horizontal confidence intervals graph.

Something like this:

enter image description here

from Statistics Globe Newsletter.


Solution

  • To answer your question, yes it is possible. Here is the data taken from the vignette example.

    # prepare data
    library(sjmisc)
    data(efc)
    efc <- to_factor(efc, c161sex, e42dep, c172code)
    m <- lm(neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, data = efc)
    
    # simple forest plot
    plot_model(m)
    

    To create rotated errorbars, you need to apply coord_fixed first as coord_flip was already applied based on the source code. Then you can rotate the x axis text to make them readable.

    p+coord_fixed() + 
      theme(axis.text.x = element_text(angle = 90))
    

    enter image description here

    To have the bars appear on the whiskers, you may need to draw over the default error bars as the width have been hard-coded.

    p+coord_fixed() + 
      theme(axis.text.x = element_text(angle = 90)) + 
      geom_errorbar(
        aes_string(ymin = "conf.low", ymax = "conf.high"),
        width = 0.25
      )
    

    enter image description here