fsolt / dotwhisker

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

relabel_predictors masks the dot_args function #74

Closed ryanburge closed 6 years ago

ryanburge commented 6 years ago

Here's a reproducible example:

https://gist.github.com/ryanburge/89e08f1966feae687b2ea0cca4cdd1de

fsolt commented 6 years ago

Thanks, Ryan, for pointing this out, and for the reprex. Sorry to take so long to really look at this.

Unfortunately, I don't see an elegant fix. When x is dwplot output, relabel_predictors() just calls dwplot() again on the plotted data (which can easily be recovered with x$data), but there's no way to get the dot_args back since one cannot recover a ggplot call from a ggplot object (which appears to still be true, despite the date on that question and Hadley's answer).1 One option would be to add a dot_args argument to relabel_predictors(), which I started to implement, but when I was writing up the help it just seemed too clunky: "If x is a plot generated by dwplot(), a list of arguments specifying changes to the appearance of the dots representing mean estimates. For supported arguments, see geom_pointrangeh" and, you know, don't specify them in your call to dwplot(). Sigh.

There is, otoh, what I think is a fairly straightforward workaround: call relabel_predictors() first and then call dwplot():

regplot <- read_csv("https://raw.githubusercontent.com/ryanburge/cces/master/regplot.csv")

regplot %>%
relabel_predictors(c(treated = "White",
                        age = "Age",
                        educ = "Education",
                        income = "Income",
                        male = "Male",
                        imp = "Importance of Religion",
                        pid = "Republican ID",
                        attend = "Church Attendance",
                        pray = "Frequency of Prayer")) %>%
dwplot(dot_args = list(size = 5))

rplot

I've added a note describing this workaround to the help for relabel_predictors.

1: I almost convinced myself into thinking I'd solved this via ggplot_build, because it worked on the reprex, but that only allows a size argument to be recovered--and not, say, shape--as far as I could tell.

ryanburge commented 6 years ago

That's a terrific (and easy) solution. Thanks very much.

fsolt commented 6 years ago

Actually, inspiration struck a couple of nights ago, and I think I have a much better solution now. Your original code should work fine with the current dev version:

screen shot 2018-04-06 at apr 6 1 49 28 pm