I'm currently working with igraph and have colour labelled my vertices. I would like to add a legend Indicating what each colour represents.
What I can think of at this point is to use ggplot2 to print only the legend and hide a bar plot. Is there a way to just output the legend?
Here are 2 approaches:
library(ggplot2) library(grid) library(gridExtra) my_hist <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
# Using the cowplot package legend <- cowplot::get_legend(my_hist) grid.newpage() grid.draw(legend)
Shamelessly stolen from: Inserting a table under the legend in a ggplot2 histogram
## Function to extract legend g_legend <- function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] legend } legend <- g_legend(my_hist) grid.newpage() grid.draw(legend)
Created on 2018-05-31 by the reprex package (v0.2.0).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With