I've been attempting to extract a ggplot2()
legend with cowplot::get_legend()
following the instructions outlined in a previous post. I aim to save this as an image to add to another plot and would like to change the background colour to "#F0F8FF"
and remove the majority of the blank space that I end up with after running grid.draw(legend)
. Is there a way to do this? Thanks.
# install.packages("cowplot")
set.seed(10)
mydata <- data.frame(
X = rnorm(80, 8, 4),
Y = rnorm(80, 7, 8)
)
p <- ggplot(mydata, aes(x = X, y = Y, colour = Y)) +
geom_point()
legend <- cowplot::get_legend(p)
grid.newpage()
grid.draw(legend)