Skip to content
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

Merge uni- and multivariate regression in plot_summs / legend override issue #161

Open
bsim95 opened this issue Nov 2, 2024 · 0 comments

Comments

@bsim95
Copy link

bsim95 commented Nov 2, 2024

Is your feature request related to a problem? Please describe.
I am currently trying to merge uni- and multivariate regression results from 6 "predictor" variables in the same forest plot using plot_summs, so I can visually summarize the individual regression of a parameter compared to a multivariate analysis.

While I know that I can set up 6 univariate functions and 1 multivariate function (with lm...) and plot them, this runs into issues when trying to set up a legend that summarizes all "univariate" into one legend item (next to the "multivariate" item). I tried several workarounds with scale_color_manual ( breaks (...)) that achieve a manual creation of a "univariate" and "multivariate" item, however, then there is still a default mode of variables (Model 1, Model 2, Model 3 ..., Model 7) that I cannot remove.

Describe the solution you'd like
Option 1 (preferred): Implement the Option to summarize a group of lm models into one group corresponding to one legend item
Option 2: Implement an option to manually decide on the legend items to be shown
Option 3: suggestion how I can fix the described problem when using the scale_color_manual function, that the default legend items (Model 1 ...) still shows up and cannot be removed

Describe any alternatives or other implementations that you might know of
See above.

Additional context
Example code on the problem, how I tried to fix it:

Load necessary libraries

library(jtools)
library(ggplot2)

Example of linear regression models

fit1 <- lm(mpg ~ wt + hp + qsec, data = mtcars)
fit2 <- lm(mpg ~ wt + hp, data = mtcars)

Create the plot_summs plot without the legend

p <- plot_summs(fit1, fit2,
model.names = c("fit1", "fit2"),
colors = c("blue", "red")) +
theme(legend.position = "none") # Remove default legend

Add the custom legend

p <- p + scale_color_manual(
name = "Models", # Name of the legend
breaks = c("fit1", "fit2"),
values = c("fit1" = "blue", "fit2" = "red"), # Adjust colors as needed
labels = c("f1", "f2") # Adjust labels as needed
) + guides(color = guide_legend(override.aes = list(linetype = c(1, 1), shape = c(16, 16))))

Position the custom legend

p <- p + theme(legend.position = "right")

print(p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant