fsolt / dotwhisker

Dot-and-Whisker Plots of Regression Results
https://fsolt.org/dotwhisker/
Other
57 stars 10 forks source link

Relabel_predictors for factor variables #107

Closed IsadoraBM closed 2 years ago

IsadoraBM commented 2 years ago

Relabel_predictors doesn't work for factor variables since it treats (for example) EducationLow as a different predictor from EducationHigh even if they are both coming from Education in lm(y~ Education, data=df) Is there any way of forcing a space between instances of lowercaseUppercase without having to manually list every term level relabel_predictors = c((EducationLow = "Education Low", EducationHigh = "Education High"))?

sammo3182 commented 2 years ago

Isadora, I can't reproduce the issue you mentioned. relabel_predictors works fine to relabel factor outputs. See the example below:

library(dotwhisker)

mod <- lm(mpg ~ wt + as.factor(cyl), data = mtcars)

# draw a dot-and-whisker plot
dwplot(mod) %>% 
    relabel_predictors(
        c(wt = "Weight", 
          `as.factor(cyl)6` = "Cylinder + 6 Gear",
          `as.factor(cyl)8` = "Cylinder + 8 Gear")
    )

image

Please be aware, the original variable labels (e.g., "EducationLow" and "EducationHigh") were produced not by dotwhisker but the regression function. R automatically pasted the categories of interacted factors together (see also the example above). If you want to have a space between the Education and low/high, you may try to leave a space before the categories when setting the factor, for example, r education <- factor(education, labels = c(" low", " medium", " high").

Free to reopen the issue if there's other things I can help to deal with this issue.