vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
392 stars 43 forks source link

Request for plot_comparisons: choosing number of columns or rows when printing #1107

Closed Crismoc closed 2 months ago

Crismoc commented 2 months ago

When printing multiple plots with plot_*, is it possible to add a simple way to choose how many columns or rows the figure should have?

I imagine something like the code below, but making it possible to specify the rows or columns like this plot_comparisons(..., ncol = 1)

library(marginaleffects)
dat <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv")

mod <- lm(body_mass_g ~ flipper_length_mm * species * bill_length_mm + island, data = dat)
plot_comparisons(mod,
                 variables = c("flipper_length_mm", "bill_length_mm"),
                 by = c("species", "island"))

vincentarelbundock commented 2 months ago

No, this is not possible. But these figures are absolutely trivial to draw once we have the proper dataset. And you can get the dataset by using draw=FALSE. After that, it should be no more than 3 lines of ggplot2 to get exactly what you want.

Crismoc commented 2 months ago

Thank you! You're right, it's possible to do it in one line relying on ggplot and finding the right name for the panels

library(ggplot2)
library(marginaleffects)
dat <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv")

mod <- lm(body_mass_g ~ flipper_length_mm * species * bill_length_mm + island, data = dat)
plot_comparisons(mod,
                 variables = c("flipper_length_mm", "bill_length_mm"),
                 by = c("species", "island")) +
  facet_wrap(~term, ncol = 1)