jacob-long / jacob-long/jtools
Merge uni- and multivariate regression in plot_summs / legend override issue
- Dominant language
- R
- Stars
- 171
- Forks
- 23
- PR merge metrics
- No merged PRs in 30d
Description
**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)
Contributor guide
Research direction
Start at the plot_summs entry point and reproduce the supplied mtcars example with the univariate and multivariate model inputs. Define the desired legend behavior from the three proposed options, then verify that grouped univariate models can share one legend item and that unwanted default model entries are absent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data-visualization
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100