From 69f65305687e89baa9d621a30b213db486b0db9c Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 16 Jan 2024 17:24:40 +0100 Subject: [PATCH] deal with multiple legends --- R/love.plot.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/love.plot.R b/R/love.plot.R index 07006506..02e1ce21 100644 --- a/R/love.plot.R +++ b/R/love.plot.R @@ -1164,7 +1164,14 @@ love.plot <- function(x, stats, abs, agg.fun = NULL, else legend.to.get <- 1 legg <- ggplot2::ggplotGrob(plots.to.combine[[legend.to.get]] + ggplot2::theme(legend.position = position)) - leg <- legg$grobs[[which(legg$layout$name == "guide-box")]] + if (any(legg$layout$name == "guide-box")) { + leg <- legg$grobs[[which(legg$layout$name == "guide-box")]] + } else if (any(legg$layout$name == paste0("guide-box-", position))) { + # ggplot2 >=3.5.0 can have multiple legends + leg <- legg$grobs[[which(legg$layout$name == paste0("guide-box-", position))]] + } else { + position <- "none" + } if (position == "left") { p <- gridExtra::arrangeGrob(grobs = list(leg, g), nrow = 1,