plot_coefs function

Hello everyone,

I'm new to this forum (and coding in R), and I have a question regarding the plot_coefs() function from the "jtools" package. I was wondering if there is an argument to show the coefs on a horizontal line instead of the default vertical line? To "flip" the plot so to speak. I have been looking through the arguments and I just can't seem to find a way to do this.

I hope you can help!
Best,
Zach

Hi @ZherlockHolm,

I looked over the manual for jtools::plot_coefs() and under the hood it is using the ggplot2 package to create the plot. If you enjoy using R it might be worth the effort to invest some time into learning ggplot2, it is somewhat technical in nature but provides you with ultimate flexibility for making any plot look exactly how you'd like with basically any kind of data. There are many good resources for learning.

All that being said, the plot_coefs() function returns a ggplot object, which means you can just add on to the plot relatively easily. If you can provide a snippet of the data you are trying to plot, I would be happy to show you how to modify the plot_coefs() object and also how to plot things directly using ggplot2.

Actually, here is the example from ?plot_coefs(). I can demonstrate what I mean using these data.
Here is the jtools way of doing things:

library(jtools)

states <- as.data.frame(state.x77)
fit1 <- lm(Income ~ Frost + Illiteracy + Murder +
             Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))

# horizontal plot
plot_coefs(fit1)

# vertical plot by flipping the coordinates
plot_coefs(fit1) + ggplot2::coord_flip()

Now, lets build the plot from scratch without using the jtools helper function:

library(ggplot2)
library(dplyr)
data <- broom::tidy(fit1, conf.int = TRUE)

# Let's recreate the plot_coefs() plot using only ggplot2 code
data %>% 
  # Remove intercept (optional)
  filter(!term %in% '(Intercept)') %>% 
  ggplot() +
  # add line at null of 0
  geom_hline(yintercept = 0, lty = 2) +
  # add error bars for confidence interval
  geom_segment(aes(x = term, xend = term,
                   y = conf.low, yend = conf.high), colour = 'dodgerblue',
               size = 0.75) +
  # add points for the coefficient estimates
  geom_point(aes(term, estimate),
             fill = 'white', colour = 'dodgerblue', shape = 21, 
             size = 3, stroke = 1) +
  # label axes
  labs(y = '', x = 'term') +
  # use built-in ggplot2 theme
  theme_minimal() +
  # add some final touches by customizing the theme
  theme(
    panel.grid = element_blank(),
    panel.grid.major.x = element_line(colour = '#dddddd'),
    axis.title.x = element_text(face = 'bold')
  )

Yes, the ggplot2 way of doing things was much more code, but I had complete control over every aspect of the plots aesthetic. Anything can be changed to your preference.

1 Like

Thank you so much for your reply, @mattwarkentin, this was very helpful!
I was able to flip the plot just by adding the ggplot2::cord_flip() part of the code, but I will definitely come back to your explanation on ggplot2 if I need to change anything else.

1 Like