How to plot just the legends in ggplot2?

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?

Answers


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


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

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)

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)

Need Your Help

Make one section of simple web page have own scroll bar

html css

I have a section of the web page I am building that is dedicated to news events. These are simply entered as follows currently.

Android Twitter integration using oauth and twitter4j

android twitter twitter-oauth twitter4j android-twitter

I want to integrate twitter in an android application and found many tutorials. Implemented 2 of them. But after implementing, when ran the application, I came to know that they use older version of