4/13/2022
40
R Plots Examples Average ratng: 4,5/5 5348 reviews

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.

Syntax of Legend function in R:

legend(x, y = NULL, legend, fill = NULL, col = par(“col”),border = “black”, lty, lwd, pch)
x, ythe x and y co-ordinates which is used to position the legend.
legenda character vector of legend names
fillfill legend box with the specified colors.
colthe color of points or lines appearing in the legend.
borderthe border color for the legend box
lty, lwdthe line types and widths for lines appearing in the legend.
pchthe 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

  1. Plot Line in R (8 Examples) Create Line Graph & Chart in RStudio. In this R tutorial you’ll learn how to draw line graphs. The article contains eight examples for the plotting of lines. To be more specific, the article looks as follows: Creating Example Data; Example 1: Basic Creation of Line Graph in R; Example 2: Add Main Title & Change.
  2. To make it easy to get started, the ggplot2 package offers two main functions: quickplot and ggplot. The quickplot function – also known as qplot – mimics R’s traditional plot function in many ways. It is particularly easy to use for simple plots. Below is an example of the default plots that qplot makes.
  3. Scatter plot with regression line. As we said in the introduction, the main use of scatterplots in R is to check the relation between variables.For that purpose you can add regression lines (or add curves in case of non-linear estimates) with the lines function, that allows you to customize the line width with the lwd argument or the line type with the lty argument, among other arguments.

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

Plot Function R

R Plots Examples

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

Specify legend position by keywords

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 :

Examples

Add legend Box and background color to legend box:

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.

R Plot Graph

so the resultant plot will have green color box, with light blue back ground

Add Title and font to the legend

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