How to plot just the legends in ggplot2?

RGgplot2Igraph

R Problem Overview


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?

R Solutions


Solution 1 - R

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: https://stackoverflow.com/questions/11883844/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).

Solution 2 - R

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

Solution 3 - R

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)

Solution 4 - R

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionButhetleonView Question on Stackoverflow
Solution 1 - RTyler RinkerView Answer on Stackoverflow
Solution 2 - Rr.botView Answer on Stackoverflow
Solution 3 - RglhrshView Answer on Stackoverflow
Solution 4 - RButhetleonView Answer on Stackoverflow