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?


Here are 2 approaches:

Set Up Plot

my_hist <- ggplot(diamonds, aes(clarity, fill = cut)) + 
Cowplot approach
# Using the cowplot package
legend <- cowplot::get_legend(my_hist)


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 <- g_legend(my_hist) 


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.

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)

I used the ggpubr package - makes it very easy!

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

# Convert to a ggplot and print

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