juba / questionr

R package to make surveys processing easier
https://juba.github.io/questionr/
81 stars 17 forks source link

Missing dependency for boxplot from ggsurvey #138

Closed mbunk closed 3 months ago

mbunk commented 3 months ago

I noticed a missing dependency for creating the boxplox from the ggsurvey function. I get an error and a blank plot when running the following code:

library(ggplot2) library(questionr) library(survey)

d <- as.data.frame(Titanic) dw <- survey::svydesign(ids = ~1, weights = ~Freq, data = d) questionr::ggsurvey(dw) + aes(y = Freq, x = Survived) + geom_boxplot()

output:

Warning message: Computation failed in stat_boxplot(). Caused by error in loadNamespace(): ! there is no package called ‘quantreg’

The boxplot only rendered after I installed and loaded the quantreg package. However, I cannot locate where this is listed as a dependency and it does not automatically attach.

My session info is:

R version 4.4.1 (2024-06-14) Platform: x86_64-pc-linux-gnu Running under: Ubuntu 20.04.6 LTS

attached base packages: [1] grid stats graphics grDevices utils datasets [7] methods base

other attached packages: [1] survey_4.4-2 survival_3.6-4 Matrix_1.7-0
[4] ggplot2_3.5.1 questionr_0.7.8

The conditions under which the boxplot will render after attaching the quantreg package are:

[1] quantreg_5.98 SparseM_1.84-2 survey_4.4-2 survival_3.6-4 Matrix_1.7-0
[6] ggplot2_3.5.1 questionr_0.7.8

Hope this was helpful to flag! Thank you for the great package.

juba commented 3 months ago

Hi,

And thanks for reporting the issue with a reproducible example. I don't think it really is a questionr issue: quantreg is not used directly by questionr, it is called by ggplot2. And it is in the Suggests dependencies of ggplot2, which means it won't be installed by default. So I think the correct way to handle this is the way you did, by installing it manually when it was requested.

Thanks !

mbunk commented 3 months ago

Thanks for the quick response!

That makes sense. I wanted to flag because I found it peculiar that my boxplots without survey weighting adjustments render fine from ggplot2 before loading quantreg,

e.g. ggplot(d) + aes(y = Freq, x = Survived) + geom_boxplot()

but the difficulty emerges with the questionr version. Works well once quantreg is loaded, so I will close the issue.

Thanks again!

juba commented 3 months ago

In fact the error is caused by adding a weights argument to your aes() definition, for example:

ggplot(d) + aes(y = Freq, x = Survived, weights=rnorm(nrow(d))) + geom_boxplot()

That's what ggsurvey does, it adds weights computed by survey, so that's why you get the error in this case.