简体   繁体   中英

Editing the appearance of the confidence intervals in a marginal effects plot

I'm producing a series of marginal effects plots from a logistic regression, using plot_model. I would like to change the appearance of the confidence intervals in the plot below, but I can't figure out a way to do it. I assume this would be through editing the ggplot theme?

Ideally, I would like to be able to make the parallel bars smaller or remove them entirely, change the line thickness, etc. If you could point me in the right direction that would be very helpful.

library(sjPlot) 
mtcars$am <- factor(mtcars$am)
m <- glm(vs ~ am, mtcars, family = 'binomial')
plot_model(m, type = "pred", terms = "am")

Output:

在此处输入图像描述

I'm new to ggplot2, so sorry if there is a simple answer to this!

Thanks

plot_model produces a ggplot object. The problem with using extension packages like sjPlot is that one has to sacrifice some of one's ability customize a plot in return for ease-of-use.

It is possible to alter a ggplot after it has been created, but it does require altering the layer specifications of the plot. This isn't too difficult if you know where to look, but for a relatively new user it can be quite intimidating.

First, store your plot:

p <- plot_model(m, type = "pred", terms = "am")

Now, if we want to change the size of the parallel bars, we can do:

p$layers[[2]]$geom_params$width <- 0.01

(Obviously to get rid of them completely set it to 0 instead of 0.01)

To change the thickness of the lines, we can do:

p$layers[[2]]$aes_params$size <- 1.4

And to change the color of the lines we do:

p$layers[[2]]$aes_params$colour <- 'deepskyblue4'

It will also look better to have the points in front of the lines rather than behind them, so we can copy the back layer to the front like this:

p$layers[[3]] <- p$layers[[1]]

That leaves us with the following plot:

p

在此处输入图像描述

However, we can still add scales, coords and themes to this plot to customize it, so for example, we might wish to do:

p + 
  theme_minimal(base_size = 20) +
  coord_cartesian() +
  theme(aspect.ratio = 1.5,
        plot.title = element_text(hjust = 0.5),
        plot.title.position = 'plot')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM