fsolt / dotwhisker

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

`dwplot` not working when `style = "distribution"` #104

Closed IndrajeetPatil closed 3 years ago

IndrajeetPatil commented 3 years ago
library(dotwhisker)
#> Loading required package: ggplot2

# run a regression model
mod <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)

# draw a dot-and-whisker plot
dwplot(mod, style = "distribution")
#> Error: Problem with `mutate()` input `n`.
#> x could not find function "n"
#> ℹ Input `n` is `1:n()`.
#> ℹ The error occurred in group 1: term = "cyl", model = "one".

Created on 2021-04-14 by the reprex package (v2.0.0)

Trace:

> rlang::last_trace()
<error/dplyr:::mutate_error>
Problem with `mutate()` input `n`.
x could not find function "n"
ℹ Input `n` is `1:n()`.
ℹ The error occurred in group 1: term = "cyl", model = "one".
Backtrace:
     █
  1. ├─dotwhisker::dwplot(mod, style = "distribution")
  2. │ └─`%>%`(...)
  3. ├─dplyr::filter(., !is.na(estimate))
  4. ├─dplyr::mutate(...)
  5. ├─dplyr:::mutate.data.frame(...)
  6. │ └─dplyr:::mutate_cols(.data, ...)
  7. │   ├─base::withCallingHandlers(...)
  8. │   └─mask$eval_all_mutate(quo)
  9. └─base::.handleSimpleError(...)
 10.   └─dplyr:::h(simpleError(msg, call))
<error/simpleError>
could not find function "n"
sammo3182 commented 3 years ago

Ahm...I can't reproduce the error Indrajee @IndrajeetPatil. I got the following outcome (you'll see I trunked the disp for its coefficient to squeeze the rest to be seen. But plot is still doable. With a wide guess, maybe some other packages overwrote the dplyr::mutate function? Free to reopen the issue if the problem still exists.

library(dotwhisker)

mod <- lm(mpg ~ wt + cyl + gear, data = mtcars)

# draw a dot-and-whisker plot
dwplot(mod, style = "distribution")

image

IndrajeetPatil commented 3 years ago

Thanks for the response.

I can still reproduce this error:

library(dotwhisker)
#> Loading required package: ggplot2

mod <- lm(mpg ~ wt + cyl + gear, data = mtcars)

# draw a dot-and-whisker plot
dwplot(mod, style = "distribution")
#> Error: Problem with `mutate()` column `n`.
#> ℹ `n = 1:n()`.
#> x could not find function "n"
#> ℹ The error occurred in group 1: term = "cyl", model = one.

Created on 2021-07-17 by the reprex package (v2.0.0)

The way to make it work is by explicitly loading dplyr:

library(dotwhisker)
#> Loading required package: ggplot2
library(dplyr)

mod <- lm(mpg ~ wt + cyl + gear, data = mtcars)

# draw a dot-and-whisker plot
dwplot(mod, style = "distribution")

Created on 2021-07-17 by the reprex package (v2.0.0)

The problem is that you are using dplyr::n() function somewhere but not using either namespace :: or importing it in your package namespace. So the user needs to do it.

sammo3182 commented 3 years ago

Ah! Got it! I've updated the namespace in the dev version cc2d53c14b3cb48591059c1ea1a8d4b9203d83c3. I've recorded the bug in the News.md for the next update. Thanks for pointing that out!

IndrajeetPatil commented 3 years ago

One more thing: This produces a cryptic warning:

#> Warning in if (is.na(out)) {: the condition has length > 1 and only the first
#> element will be used

Reproducible example:

library(dotwhisker)
#> Loading required package: ggplot2
# define a regression model
m<- lm(mpg ~ wt * cyl, data = mtcars)

# draw a dot-and-whisker plot
dwplot(m, style = "distribution")
#> Warning in if (is.na(out)) {: the condition has length > 1 and only the first
#> element will be used

Created on 2021-09-13 by the reprex package (v2.0.1)