Describe Parts of a Chart in Graphical Form in R Programming - legend() Function
Last Updated :
24 Nov, 2023
legend() function in R Programming Language is used to add legends to an existing Plot. A legend is defined as an area of the graph plot describing each of the parts of the plot. The legend plot is used to show statistical data in graphical form.
Syntax: legend(x, y, legend, fill, col, bg, lty, cex, title, text.font, bg)
Parameters:
- x and y: These are co-ordinates to be used to position the legend
- legend: Text of the legend
- fill: Colors to use for filling the boxes of legend text
- col: Colors of lines
- bg: It defines background color for the legend box.
- title: Legend title (optional)
- text.font: An integer specifying the font style of the legend (optional)
- Returns: Legend plot
R - legend() Example
Basic example of legend() function in R
R
# Generate some data
x <- 1:10
y1 <- x * x
y2 <- 2 * y1
# Create a plot with two lines
plot(x, y1, type = "b", pch = 19, col = "green", xlab = "X", ylab = "Y")
lines(x, y2, pch = 22, col = "darkgreen", type = "b", lty = 6)
# Add a basic legend
legend("topright", legend = c("Line 1", "Line 2"), col = c("green", "darkgreen"),
lty = 1:2)
Output:
Describe Parts of a Chart in Graphical Form in R Programming - legend() Function
Example 2: Adding Title, text font, and background color of the legend box
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 19,
col = "green",
xlab = "X", ylab = "Y")
lines(x, y2, pch = 22, col = "darkgreen",
type = "b", lty = 6)
}
makePlot()
# Add a legend to the plot
legend(1, 95, legend = c(" Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.9,
title = "Line types", text.font = 6, bg = 'gray')
Output:

Here, the legend() function is used to add a legend to the plot, and makePlot() function is used to manipulate font, background color.
To create a border of the legend box
Syntax: legendx, y, fill, col, bg, lty, cex=0.8, box.lty, box.lwd, box.col)
Parameters:
box.lty, box.lwd and box.col: line type, width and color for the legend box border, respectively.
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Change the border
makePlot()
legend(1, 100, legend = c("Legend Line 1", "Legend Line 2"),
col = c("green", "darkgreen"), lty = 1:2, cex = 0.8,
box.lty = 4, box.lwd = 2, box.col = "green")
Output:

Remove legend border by using box.lty = 0 in legend() function
Syntax: legendx, y, fill, col, bg, lty, cex=0.8, box.lty=0)
Parameter: box.lty: Box line width
R
makePlot<-function(){
x<-1:10;
y1 = x * x; y2 = 2 * y1
plot(x, y1, type = "b", pch = 22,
col = "green",
xlab = "x", ylab = "y")
lines(x, y2, pch = 18, col = "darkgreen",
type = "b", lty = 4)
}
# Remove legend border using box.lty = 0
makePlot()
legend(2, 100, legend = c("Legend Line 1",
"Legend Line 2"),
col = c("green", "darkgreen"),
lty = 1:2, cex = 0.8, box.lty = 0)
Output:

In example 3 and example 4 box.lty, box.lwd, and box.col can be used to modify the line type, width, and color for the legend box border, respectively are used to modify the arguments.
Horizontal Legend with Different Symbols
R
# Create a plot with two lines
makePlot()
# Add a horizontal legend with different symbols
legend("bottom", legend = c("Line 1", "Line 2"),
col = c("green", "darkgreen"), lty = 1:2, pch = c(19, 22), horiz = TRUE)
Output:
Describe Parts of a Chart in Graphical Form in R Programming - legend() Function
makePlot()
function to create a plot with two lines. Subsequently, a horizontal legend is added at the bottom of the plot using the legend()
function. The legend includes labels "Line 1" and "Line 2," corresponding colors, line types, and different symbols (plotting characters) for each line. The horiz = TRUE
argument ensures a horizontal layout for the legend.
Similar Reads
Create Dot Charts in R Programming - dotchart () Function dotchart() function in R Language is used to create a dot chart of the specified data. A dot chart is defined as a plot which is used to draw a Cleveland dot plot. Syntax: dotchart(x, labels = NULL, groups = NULL, gcolor = par("fg"), color = par("fg")) Parameters: x: it is defined as numeric vector
2 min read
Create a Plot Matrix of Scatterplots in R Programming - pairs() Function pairs() function in R language is used to return a plot matrix, consisting of scatter plots corresponding to each data frame. R - Create Plot Matrix of Scatterplots Syntax: pairs(data) Parameters: data: It is defined as  value of pairs Plot. Returns: Color, Labels, Panels, and by Group in pairs plo
2 min read
Plotting of Data using Generic plots in R Programming - plot() Function In this article, we will discuss how we plot data using Generic plots in R Programming Language using plot() Function. plot functionplot() function in R Programming Language is defined as a generic function for plotting. It can be used to create basic graphs of a different type. Syntax: plot(x, y, t
5 min read
Adding axis to a Plot in R programming - axis () Function axis() function in R Language is to add axis to a plot. It takes side of the plot where axis is to be drawn as argument. Syntax: axis(side, at=NULL, labels=TRUE) Parameters: side: It defines the side of the plot the axis is to be drawn on possible values such as below, left, above, and right. at: Po
2 min read
Change the position and the appearance of a graph legend 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 a legend to the plot. Sc
4 min read