Open In App

How to Draw a Legend Outside of a Plot in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 plotting clipped to the figure region.

Syntax:

par( mar, xpd)

where,

  • mar: determines the vector which contains margin.
  • xpd: It is a boolean. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region

Then we use the legend() function to add a legend layer on top of it. To put the legend at the desired position on the plot we use the inset parameter of the legend function.

Syntax:

legend(position, inset, title, legend, pch, col )

where,

  • position: determines the position of the legend.
  • inset: determines the shift in position.
  • title: determines the title of legend.
  • pch: determines the symbols used to represent the data point.
  • col: determines the color of data points.

Example 1:

Here, is a basic plot in the R Language with the legend on the top right corner of the plot.

R




# create sample data frame
sample_data <- data.frame(x = c(rnorm(50), rnorm(50, 5)),      
                   y = c(rnorm(50), rnorm(50, 5)),
                   group = c(rep(1, 50), rep(2, 50))) 
  
# create margin around plot
par(mar = c(3, 3, 3, 8), xpd = TRUE)
  
# Draw scatter plot
plot(sample_data$x, sample_data$y,                                   
     pch = sample_data$group+10,
     col = sample_data$group)
  
# Draw legend
legend("topright", inset = c(-0.3, 0.1),                   
       legend = c("Group 1","Group 2"),
       pch = c(11,12), col = 1:2)


Output:

Example 2:

Here, is a basic plot in the R Language with the legend on the bottom of the plot.

R




# create sample data frame
sample_data <- data.frame(x = c(rnorm(50), rnorm(50, 5), rnorm(50, 10)),      
                   y = c(rnorm(50), rnorm(50, 5), rnorm(50, 10)),
                   group = c(rep(1, 50), rep(2, 50), rep(3, 50))) 
  
# create margin around plot
par(mar = c(10, 3, 3, 3), xpd = TRUE)
  
# Draw scatter plot
plot(sample_data$x, sample_data$y,                                   
     pch = sample_data$group+10,
     col = sample_data$group)
  
# Draw legend
legend("topright", inset = c(0.4, 1.2),                   
       legend = c("Group 1","Group 2","Group 3"),
       pch = c(11,12,13), col = 1:3)


Output:



Similar Reads

Draw ggplot2 Legend without Plot in R
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() fu
3 min read
Set Aspect Ratio of Scatter Plot and Bar Plot in R Programming - Using asp in plot() Function
asp is a parameter of the plot() function in R Language is used to set aspect ratio of plots (Scatterplot and Barplot). Aspect ratio is defined as proportional relationship between width and height of the plot axes. Syntax: plot(x, y, asp ) Parameters: x, y: Coordinates of x and y axis asp: Aspect ratio Example 1: # Set seed for reproducibility set
1 min read
Plot Paired dot plot and box plot on same graph in R
R Programming Language is used for statistical computing and graphics. R was first developed at the University of Auckland by two professors Ross Ihanka and Robert Gentleman Dot Plot The dot plot is a graphical representation of how one attribute varies with respect to another attribute. On the x-axis, we usually plot the attribute with respect to
7 min read
Annotate Text Outside of ggplot2 Plot in R
Ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system. There are many scenarios where we need to annotate outside the plot area or specific area as per client requirements. In this case, the ggplot
2 min read
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
Add Legend to Plot in R
Legends are useful to add more information to the plots and enhance the user readability. It involves the creation of titles, indexes, placement of plot boxes in order to create a better understanding of the graphs plotted. The in-built R function legend() can be used to add legend to plot. Syntax: legend(x, y, legend, fill, col, bg, lty, cex, titl
4 min read
Change Legend Size in Base R Plot
In this article, we will be looking at the approach to change the size of the legend in the plot in the R programming language. To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the pl
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
Article Tags :