####################################################################################################################################
################################## grabLegend ######################################################################################
# >>
grabLegend <- function(plot) {
tmp = ggplot_gtable(ggplot_build(plot))
leg = which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend = tmp$grobs[[leg]]
if ( Sys.getenv("RSTUDIO") == "1" ) {
dev.set(2)
dev.off()
dev.new()
}
return(legend)
}
# <<
################################## grabLegend ######################################################################################
####################################################################################################################################
####################################################################################################################################
################################## Help Code #######################################################################################
# >>
# legend = grabLegend(ggplot)
# <<
################################## Help Code #######################################################################################
####################################################################################################################################
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.