Search code examples
rggplot2linear-regressionsjplot

How to adjust x axis in coefficient plot with sjPlot's plot_model when x value range is smaller than -1,1?


I'm plotting multiple regression coefficients with plot_model and the x axis is automatically set to -1,1 even though the possible range of my values is smaller, which makes the estimates hard to see and differentiate. Therefore, I want to adjust the x axis, e.g. to -0.5,0.5.

library(sjPlot)
data(efc)

model <- lm(quol_5/10 ~ c12hour + e15relat + e16sex + e42dep, data = efc) #dividing the variable quol_5 by 10 #to show the issue with small outcome values, as is the case in my actual data
plot <- plot_model (model, type = "est") 

coefficient plot, the differences between coeffienct estimates are barely visible due to the scale mismatch between the coefficient range and the x axis limits

I've tried adjusting the x axis to -0.5,0.5 with scale_x_continuous, which results in the error message "Error in scale_x_continuous(): ! Discrete values supplied to continuous scale. ℹ Example values: c12hour, e15relat, e16sex, and e42dep" plot + scale_x_continuous(limits = c(-0.5, 0.5))

What is the problem here, and how can I adjust the x axis to my preferred range?

I am aware of the solution to supply the range of variables when plotting predicted values as discussed here: R || Adjusting x-axis in sjPlot::plot_model() Is there any way to apply this to plotting regression coefficients (i.e. specifying a range for the coefficients) and if so, how?


Solution

  • As plot_model uses coord_flip under the hood you have to set the limits= for the "x" axis using scale_y_continuous:

    library(sjPlot)
    #> Learn more about sjPlot with 'browseVignettes("sjPlot")'.
    library(ggplot2)
    data(efc)
    
    model <- lm(quol_5 / 10 ~ c12hour + e15relat + e16sex + e42dep, data = efc)
    plot <- plot_model(model, type = "est")
    
    plot +
      scale_y_continuous(limits = .5 * c(-1, 1))
    #> Scale for y is already present.
    #> Adding another scale for y, which will replace the existing scale.