manually order coefficients in coefplot

599 views Asked by At

I would like to manually sort the coefficients in a coefplot. Previous questions only achieve ordering by (decreased) magnitude, which is not what I want.

Consider the below example:

library(coefplot)
data(tips, package = "reshape2")
mod1 <- lm(tip ~ day + sex + smoker + sex * smoker, data = tips)
coefplot.glm(mod1)

How would I achieve a coefficient order with sexMale:smokerYes on top and daySat, dayThu, daySun (and then the rest) without relying on their magnitude?

Thanks in advance for pointing me to a solution. :)

1

There are 1 answers

0
the-mad-statter On BEST ANSWER

Because coefplot.glm() is outputting a ggplot object, you can access the data object and re-level the y factor:

Create ggplot object

library(coefplot)
data(tips, package = "reshape2")
mod1 <- lm(tip ~ day + sex + smoker + sex * smoker, data = tips)
gg <- coefplot.glm(mod1)
gg

enter image description here

Re-level y factor

gg$data$Coefficient <- factor(gg$data$Coefficient, levels = rev(c("sexMale:smokerYes", "daySat", "dayThur", "daySun", "smokerYes", "sexMale", "(Intercept)")))
gg

enter image description here