Open In App

Draw ggplot2 Legend without Plot in R

Last Updated : 23 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A legend in the graph describes each part of the plot individually and is used to show statistical data in graphical form. In this article, we will see how to draw only the legend without a plot in ggplot2. 

First, let us see how to draw a graph with a legend so that the difference is apparent. For that load the ggplot2 package by using library() function and create a DataFrame. To create an R plot, we use ggplot() function and for getting a scatter plot we add geom_point() function to ggplot() function. 

Example:

R




# Load Package
library("ggplot2")
  
# Create a DataFrame 
data <- data.frame(
  Xdata = rnorm(10),                        
  Ydata = rnorm(10),
  LegendData = c("ld-01", "ld-02", "ld-03",
                 "ld-04", "ld-05", "ld-06",
                 "ld-07", "ld-08", "ld-09"
                 "ld-10"))
  
# Create a Scatter Plot and assign it 
# to gplot data object
gplot <- ggplot(data, aes(
  Xdata, Ydata, color = LegendData)) +   
  geom_point(size = 7)
gplot


Output:

Scatter Plot with legend

Scatter Plot with legend

Packages Used:

Now, to draw only the legend of the plot without the plot, we have to load three packages named grid, gridExtra, and cowplot.

  • grid package is used to produce a graphical output directly

Syntax:

install.packages(“grid”) #For Install grid package

library(“grid”) #For Load grid package

  • gridExtra is an extension to grid package, it supports the creation and editing of graphic objects but with higher-level functions than grid.

Syntax:

install.packages(“gridExtra”) #For Install gridExtra package

library(“gridExtra”) #For Load gridExtra package

  • cowplot package is used to enhance the presentability of figure by fixing its core components.

Syntax:

install.packages(“cowplot”) #For Install cowplot package

library(“cowplot”) #For Load cowplot package

Functions Used:

To draw only legend, we use three functions, which are from the above packages.

  • get_legend() function is used to hold the legend to a plot.

Syntax : get_legend(ggplot)

Parameter: A ggplot, from which to retrieve the legend

Return : Legend from plot

  • grid.newpage() moves the grid to another device by removing it from the former device.

Syntax : grid.newpage()

Return : Erases current plot window and create a new plot window

  • grid.draw() produces graphical output from a graphical object

Syntax : grid.draw(legend)   

Return : Draw the legend to new plot window

Thus, in order to draw a plot with just legend, first, a legend is drawn and held to the plot using get_legend(), then the plot is erased using grid.newpage() and then the legend is drawn to a new plot window using grid.draw().

Example:

R




# Load Packages
library("ggplot2")
library("grid")
library("gridExtra")
library("cowplot")
  
# Create a DataFrame
data <- data.frame(
  Xdata = rnorm(10), Ydata = rnorm(10),
  LegendData = c("ld-01", "ld-02", "ld-03",
                 "ld-04", "ld-05", "ld-06",
                 "ld-07", "ld-08", "ld-09"
                 "ld-10"))
  
# Create a Scatter Plot
gplot <- ggplot(data, aes(Xdata, Ydata, color = LegendData)) +   
  geom_point(size = 7)
  
# Draw Only Legend without plot
# Grab legend from gplot
legend <- get_legend(gplot)                    
  
# Create new plot window
grid.newpage()                              
  
# Draw Only legend 
grid.draw(legend) 


Output:

Only Legend Without Plot

Only Legend Without Plot



Similar Reads

How to move a ggplot2 legend with multiple rows to the bottom of a plot in R
In this article, we are going to see how to draw ggplot2 legend at the bottom and with two Rows in R Programming Language. First, we have to create a simple data plot with legend. Here we will draw a Simple Scatter plot. Loading Library First, load the ggplot2 package by using library() function. library("ggplot2") Create a DataFrame for example. H
3 min read
Control Line Color and Type in ggplot2 Plot Legend in R
In this article, we will see how to control line color and type in ggplot2 plot legend in the R programming language. Using Default Parameters In this method, inbuilt attributes are passed to the function with appropriate values to generate the requirement. Thus, in order to change the color, col or color attribute has to be passed with the column
2 min read
Create Legend in ggplot2 Plot in R
In this article, we will discuss how to create a legend in ggplot using R programming language. To draw a legend within ggplot the parameter col is used, it basically adds colors to the plot and these colors are used to differentiate between different plots. To depict what each color represents a legend is produced by ggplot. col attribute can be s
2 min read
Change Display Order of ggplot2 Plot Legend in R
In this article, we will see how to change display order of ggplot2 legend in R Programming Language. For that, first we should load ggplot2 package using library() function. Syntax for loading or installing ggplot2 package is given below. to install ggplot2 package, write following command to R Console. install.packages("ggplot2") library("ggplot2
3 min read
Change Spacing Between Horizontal Legend Items of ggplot2 Plot in R
In this article, we are going to see how to change the space between horizontal legend items using the ggplot2 bar plot in R programming language. Here we are using a bar graph the same can be done to any other plot. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : S
2 min read
Modify axis, legend, and plot labels using ggplot2 in R
In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : Set the stat parameter to identify the mode.fill : Represents color inside t
5 min read
Divide Legend of ggplot2 Plot in R
In this article, we will discuss how to divide the legend of ggplot2 plot in the R programming language. To divide the legend of the ggplot2 plot, the user needs to install and import the gridExtra and cowplot packages in the R console. gridExrta package: Provides a number of user-level functions to work with "grid" graphics, notably to arrange mul
2 min read
Set Legend Alpha of ggplot2 Plot in R
In this article, we are going to see how to set the legend alpha of the ggplot2 Plot in R Programming Language. Setting the legend alpha of the plot using the alpha argument of the guide_legend function from the ggplot2 package. Syntax: guide_legend(override.aes = list(alpha)) Parameters: override.aes: allows the user to change only the legend appe
2 min read
How to Draw a Legend Outside of a Plot in R?
In this article, we will discuss how to draw a legend outside of a Plot in the R Programming Language. We first create a basic plot without legend using the plot() function and add a margin around it for legend by using the par() function of the R Language. We will create the required margin and make the xpd parameter TRUE. This will make our plott
2 min read
Set Axis Limits of ggplot2 Facet Plot in R - ggplot2
In this article, we will discuss how to set the axis limits of the ggplot2 facet plot in the R programming language. Method 1: Set axis limits of ggplot2 facet plot with Free Scales Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales fu
5 min read
Article Tags :