45

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?

Burkhard
  • 14,112
  • 22
  • 84
  • 106
Buthetleon
  • 937
  • 3
  • 11
  • 20
  • Oh yeah I didn't see you said igraph. That's in base if I'm not mistaken and ggplot2 uses grid, better just make your own legend as Gabor discusses below. he obviously read more thoroughly than I did. – Tyler Rinker Aug 21 '12 at 02:13

4 Answers4

68

Here are 2 approaches:

Set Up Plot

library(ggplot2) 
library(grid)
library(gridExtra) 

my_hist <- ggplot(diamonds, aes(clarity, fill = cut)) + 
    geom_bar() 

Cowplot approach

# Using the cowplot package
legend <- cowplot::get_legend(my_hist)

grid.newpage()
grid.draw(legend)

Home grown approach

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).

Tyler Rinker
  • 99,090
  • 56
  • 292
  • 477
  • although i cannot edit the size of both legend and igraph object using the viewport function – Buthetleon Aug 21 '12 at 06:09
  • 5
    My legend has huge margins that get in the way, am I forgetting something? – Lilith-Elina Jun 26 '14 at 13:48
  • Have a look at [theme](http://docs.ggplot2.org/current/theme.html) and change the transparency of the `legend. background`. If this does not help please ask your own question with a reproducible example and the code you're trying. – Tyler Rinker Jun 26 '14 at 13:53
  • I would recommend highlighting the `cowplot` option as it is the most straightforward and efficient answer. – Garini Mar 26 '19 at 11:14
24

Cowplot handily adds a function to extract the legend. The following is taken directly from the manual.

library(ggplot2)
library(cowplot)
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5)

# Note that these cannot be aligned vertically due to the legend in the plot.mpg
ggdraw(plot_grid(p1, plot.mpg, ncol=1, align='v'))

# now extract the legend
legend <- get_legend(plot.mpg)

# and replot suppressing the legend
plot.mpg <- plot.mpg + theme(legend.position='none')

# Now plots are aligned vertically with the legend to the right
ggdraw(plot_grid(plot_grid(p1, plot.mpg, ncol=1, align='v'),
                 plot_grid(NULL, legend, ncol=1),
                 rel_widths=c(1, 0.2)))
r.bot
  • 4,775
  • 1
  • 28
  • 41
9

I used the ggpubr package - makes it very easy!

https://rpkgs.datanovia.com/ggpubr/reference/get_legend.html

# Extract the legend. Returns a gtable
leg <- get_legend(p)

# Convert to a ggplot and print
as_ggplot(leg)
glhrsh
  • 163
  • 1
  • 10
3

I was color coding the vertices in the graph and wanted to generate a legend as simply , elegantly and as quickly as I can.

The fastest way to do this I've come to believe is generate the legend separately using ggplot2 before "pasting" the legend into the same plot as igraph using viewport and layout()

In this method there is no need to call the rescale or asp arguements in the plot.igraph() function.

Using the g_legend function on a data.frame, leg, with 2 columns, x being the appropriate vertex attribute and y being the hex colour code used in my igraph plot, I've done the following.

My igraph object is t8g

legend <- g_legend(leg)
vpleg <- viewport(width = 0.1, height = 0.1, x=0.85,y=0.5)
layout(matrix(c(1,2),1,2,byrow=T),widths=c(3,1))
plot(t8g,edge.width=1,edge.arrow.size=0.1,vertex.label.cex=0.2,main="b2_top10")
pushViewport(vpleg)
grid.draw(legend)
Buthetleon
  • 937
  • 3
  • 11
  • 20