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. :)
Because coefplot.glm()
is outputting a ggplot
object, you can access the data object and re-level the y factor:
ggplot
objectlibrary(coefplot)
data(tips, package = "reshape2")
mod1 <- lm(tip ~ day + sex + smoker + sex * smoker, data = tips)
gg <- coefplot.glm(mod1)
gg
gg$data$Coefficient <- factor(gg$data$Coefficient, levels = rev(c("sexMale:smokerYes", "daySat", "dayThur", "daySun", "smokerYes", "sexMale", "(Intercept)")))
gg