I'm currently working on creating some complex visualizations using the plot_model
function from the sjPlot
package in R. Specifically, I'm dealing with interaction plots of linear models, and I'm facing an issue related to customizing the legend.
When I plot it, the keys of the legend appear inside squares, and I would like to get rid of those squares. I've read through the documentation, tried various approaches, and searched through existing questions on Stack Overflow, but I'm still struggling to achieve that.
I've attempted several approaches to remove the squares and borders from the legend in my plot. These include using the theme(legend.key = element_blank())
and theme(legend.background = element_rect(color = NA))
functions to make the legend keys transparent and remove the border lines, but unfortunately, these solutions didn't yield the desired result. They either removed the legend keys entirely or retained a gray background with black keys.
I've also experimented with guides(color = guide_legend(override.aes = list(fill = NULL, color = NULL)))
and guides(color = guide_legend(override.aes = list(fill = NULL, line = 0)))
to control the legend aesthetics, but these approaches did not fix it either.
Some of the posts that I have checked are: https://stackoverflow.com/a/11270029/18255654 and https://stackoverflow.com/a/2249622/18255654
This is the figure that I am currently obtaining:
To test the script and post it here, I am using two available datasets, and I am using the following script:
rm(list=ls())
library(nlme)
library(lme4)
library(ggplot2)
library(sjPlot)
library(ggthemes) # Library of themes for ggplot
# Load dataset for:
data=sleepstudy # Example 1
data(efc) # Example 2
# Example 1
group_colors <- c(old = "#3b71cd", young = "#c0393d")
threshold_subject_number <- 9
data$Subject <- as.numeric(data$Subject)
data$Group <- ifelse(data$Subject <= threshold_subject_number, "young", "old")
analysis_model = lme(Reaction ~ Days * Group,
data=data,
random=~1+Days|Subject,
control = lmeControl(opt = "optim"),
method = "REML")
line_plot =
plot_model(analysis_model, # Model to plot
type = "int", # Type of plot: int = interaction
title = "Title", # Add title
colors = group_colors, # Define the colors
axis.title = c("Day", "Predicted Value")) +
theme_hc() +
theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
theme(legend.position = "right") + # Move legend to right side
guides(color = guide_legend(override.aes = list(fill = NA, # Make square transparent
size = 2), # Change size of square
reverse = TRUE)) # Reverse order of legend
# Example 2
m = glm(tot_sc_e ~ c161sex + c172code * neg_c_7,
data = efc,
family = poisson())
mp = plot_model(m, type = "int") +
theme_hc() + # Change theme
theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
theme(legend.position = "right") + # Move legend to right side
guides(color = guide_legend(override.aes = list(fill = NA, # Make square transparent
size = 2), # Change size of square
reverse = TRUE)) # Reverse order of legend
## Different things I've tried:
# From: https://stackoverflow.com/a/11270029/18255654
mp +
theme(legend.key = element_blank()) # Does not work - nothing happens
# From: https://stackoverflow.com/a/2249622/18255654
mp +
theme(legend.background = element_rect(color = NA)) # Does not work - nothing happens
mp +
theme(legend.key = element_rect(colour = NA, fill = NA)) # Does not work - nothing happens
mp +
guides(color = guide_legend(override.aes = list(fill = NA, color = NA))) # Does not work - removes also the keys
mp +
guides(color = guide_legend(override.aes = list(fill = NULL, color = NULL))) # Does not work - removes line but turns all keys to black and restores grey background
mp +
guides(color = guide_legend(override.aes = list(fill = NULL, line = 0))) # Does not work - removes line but turns all keys to black and restores grey background
The outlines are caused by the geom_ribbon
used under the hood to draw the confidence band. One option to get rid of the outlines would be to manipulate the ggplot
object returned by plot_model
, i.e. by setting show.legend=FALSE
for this layer aka the second layer using mp$layers[[2]]$show.legend <- FALSE
:
library(ggplot2)
library(sjPlot)
library(ggthemes) # Library of themes for ggplot
mp <- plot_model(m, type = "int") +
theme_hc() + # Change theme
theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
theme(legend.position = "right") + # Move legend to right side
guides(color = guide_legend(
override.aes = list(
fill = NA, # Make square transparent
size = 2
), # Change size of square
reverse = TRUE
)) # Reverse order of legend
mp$layers[[2]]$show.legend <- FALSE
mp