Legend function in R adds legend box to the plot. legend() function in R makes graph easier to read and interpret in better way. lets see an example on how to add legend to a plot with legend() function in R.
x, y | the x and y co-ordinates which is used to position the legend. |
legend | a character vector of legend names |
fill | fill legend box with the specified colors. |
col | the color of points or lines appearing in the legend. |
border | the border color for the legend box |
lty, lwd | the line types and widths for lines appearing in the legend. |
pch | the plotting symbols appearing in the legend |
Example of Legend function in R:
Let’s depict how to create legend in R with an example. Before that lets create basic scatter plot using plot() function with red colored rounded dots as shown below.
To plot multiple lines in one chart, we can either use base R or install a fancier package like ggplot2. Here are two examples of how to plot multiple lines in one chart using Base R. Example 1: Using Matplot. If you have a dataset that is in a wide format, one simple way to plot multiple lines in one chart is by using matplot. Shiny Demos are a series of apps created by the Shiny developers and are designed to highlight specific features of the shiny package. Each app is presented along with its source code to help you implement these features in your apps.
output will be
Now, lets again add an another sets of scatter plot with point function with blue color pyramids as shown below.
So the resultant chart will be
Add legend to the top left corner of the plot with legend function in R:
Now let’s add the legend to the above scatter plot with legend function in R, to make it more readable
In the above function we have added legend to the top left corner of the graph at co-ordinates x= -3 and y=7 so the output will be
Add legend to the top right corner of the plot with legend function in R:
In the above function we have added legend to the top right corner of the graph at co-ordinates x= 4 and y=7 so the output will be
The position of the legend can be specified also using the following keywords : “bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right” and “center”.
The effect of using each of these keywords are shown in the figure below :
A Green color box is added to the legend using box.col=”green”. and the background of this box is filled with light blue using bg= “lightblue” as shown below.
so the resultant plot will have green color box, with light blue back ground
Title is added to the legend using Title keyword. Font of that title can be assigned with text.font keyword.
title: The title of the legend
text.font: an integer specifying the font style of the legend text; possible values are :
1: normal
2: bold
3: italic
4: bold and italic
so the resultant plot will have green color box, with light blue back ground, With Bold italic title as legend on the top left corner is shown below
for further details kindly refer here