-
-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: multiple grouping variables #279
Comments
Yeah unfortunately it's not straightforward to add a second grouping variable right now, but this is definitely a feature I'd like to add at some point. Until then, I think you can kind of hack it. Here's one way: library(bayesplot)
library(dplyr) # will use %>% and left_join
# - use the ppc_data function to get the data baseplot used internally (this is different than the data
# constructed by ggplot2 in base.plot$data)
# - add the second grouping variable (in this case cyl) and join by y_id
# - in the new data frame plot_data now has a variable "group2" now (it already had a variable "group")
plot_data <-
ppc_data(y = mtcars$mpg, yrep = ppc, group = mtcars$gear) %>%
left_join(data.frame(group2 = mtcars$cyl, y_id = 1:nrow(mtcars)), by = "y_id")
head(plot_data)
Now we can replace the data in # - use %+% to replace the data in base.plot with plot_data
# - then add facet_grid or facet_wrap (I'm not sure which you want here, maybe facet_grid actually)
base.plot %+% plot_data + facet_grid(c("group", "group2")) All together the code is: plot_data <-
ppc_data(y = mtcars$mpg, yrep = ppc, group = mtcars$gear) %>%
left_join(data.frame(group2 = mtcars$cyl, y_id = 1:nrow(mtcars)), by = "y_id")
base.plot %+% plot_data + facet_wrap(c("group", "group2")) There may be a better way to do this, but I think this should at least work. In the future hopefully we can add an argument to the plotting function for a second grouping variable or allow a different way to specify multiple grouping variables. |
Thanks! Would be a cool feature for diagnosing model misspecifications, but in the meantime this worked. |
@jgabry How would you do the same but for |
Hi, thanks so much for the package, it's a huge help! I am cross-posting this issue on the stan forums in case there is a solution I am missing. Essentially I am trying to visualize the posterior predictive distributions across two grouping variables instead of one. I am using
bayesplot::ppc_violin_grouped()
which is working like a charm for the first grouping variable, but then I'd like to facet by the second grouping variable.I thought I might be able to just add this to an existing plot using
facet_wrap()
. I looked at thedata
component of the bayesplot object and first tried to emulate that. However, I got an error that the length was wrong. If I then change the length of the facet variable to what the error asks for, it then wants a different length. Minimally reproducible example:The text was updated successfully, but these errors were encountered: