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

 `# 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

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.

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.

Previous
Next