library(ggplot2)
library(patchwork)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
# Add a discrete standalone legend to a plot
leg1 <- make_gspace_legend(c(A="red", B="blue"), legend_size = 5)
p + leg1 + plot_layout(widths = c(1, 0.1))
# Add a continuous standalone legend to a plot
leg2 <- make_gspace_legend(c("blue", "white","red"), breaks = c(0, 1, 2))
p + leg2 + plot_layout(widths = c(1, 0.1))
# Add a legend list to a plot
legs <- list(leg1, leg2)
add_gspace_legend(plot = p, legs, position = "right")
Run the code above in your browser using DataLab