The Scatter Plot in R Programming is very useful to visualize the relationship between two sets of data. The data is displayed as collection of points that shows the linear relation between those two data sets. A Scatter Plot is also called as scatter chart, scatter graph, scatter diagram, or scatter gram. For example, If we want to visualize the Age against Weight then we can use this Scatter Plot. In this article we will show you, How to Create Scatter Plot, Format its color, shape, and adding the linear progression of Scatter Plot in R Programming language with example.

## Scatter Plot in R Syntax

The basic syntax to draw the scatter chart, or Scatter Plot in R Programming is as shown below

1 |
plot(x, y = NULL, xlim = NULL, ylim = NULL, main = NULL) |

and the complex syntax behind this Scatter Plot is:

1 2 3 4 |
plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA,..) |

There are many arguments supported by the Scatter Plot in R programming language, and following are some of the arguments in real-time:

**x, y:**Please specify the data sets you want to compare. Here, you can use two seperate vectors, or Matrix with columns, or lists.**type:**Please specify, what type of plot you want to draw.- To draw Points use type = “p”
- To draw Lines use type = “l”
- Use type = “h” for Histograms
- Use type = “s” for stair steps
- To draw over-plotted use type = “o”

**sub:**You can provide the subtitle (if any) for your scatter plot.**log:**You have to specify a character string of three options. If X-Axis is to be logarithmic then**“x”,**If Y-Axis is to be logarithmic then**“y”**, if both X-Axis and Y-Axis are to be logarithmic then specify either “xy” or “yx”**axes:**It is a Boolean argument. If it is TRUE, axis is drawn.**frame.plot:**It is a Boolean argument that specifies, whether a box should be drawn around the plot or not.**panel.first:**Please specify an expression that is evaluated after the axes is drawn but before the points are plotted.**panel.last:**Please specify an expression that is evaluated after the points are plotted.**asp:**Please specify the aspect ratio of the plot (as y/x).

## Create a Scatter Plot in R Programming

In this example we will show you, How to create a scatter Plot using the faithful data set, which is provided by the R Studio. If your requirement is to import data from external files then I suggest you to refer R Read CSV article to understand the steps involved in csv file import

**R CODE**

1 2 3 4 5 6 7 8 |
# R Scatter Plot Example faithful # Finding the Correlation cor(faithful$eruptions, faithful$waiting) # Drawing Scatter Plot plot(faithful$eruptions, faithful$waiting) |

**OUTPUT**

**ANALYSIS**

Following statement will find the correlation between the eruptions, and waiting

1 |
cor(faithful$eruptions, faithful$waiting) |

* faithful* data set will return the output as a List. So, we are using the

**$**to extract the data from List.

1 |
plot(faithful$eruptions, faithful$waiting) |

### Assigning names to Scatter plot in R Programming

In this example we will show you, How to assign names to scatter plot, X-Axis, Y-Axis, and individual bars using **main****, xlab**, and** ylab**

**main:**You can provide the Title for your scatter plot.**xlab:**Please specify the label for the X-Axis**ylab:**Please specify the label for the Y-Axis**las:**It is used to change the Y axis values direction

**R CODE**

1 2 3 4 5 6 7 8 9 |
# R Scatter Plot - Changing Names Example faithful # Drawing Scatter Plot plot(faithful$eruptions, faithful$waiting, main = "R Scatter Plot", xlab = "Eruptions", ylab = "Waiting", las = 1) |

**OUTPUT**

### Change Colors of Scatter plot in R

In this example we will show you, How to change the scatter plot color using * col* argument, and size of the character that represents the point using

*argument.*

**cex****col:**Please specify the color you want to use for your Scatter plot.**cex:**Please specify the size of the point(s)

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 |
# R Scatter Plot - Changing Color, Dot Size Example faithful # Drawing Scatter Plot plot(faithful$eruptions, faithful$waiting, col = "chocolate", cex = 1.2, main = "R Scatter Plot", xlab = "Eruptions", ylab = "Waiting", las = 1) |

**OUTPUT**

### Change Shapes and Axis limits of Scatter Plot in R

In this example we will show you, How to change the shape using * pch* argument.

**xlim:**This argument can help you to specify the limits for the X-Axis**ylim:**This argument may help you to specify the Y-Axis limits

**R CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
# R Scatter Plot - Changing X, Y Limitations, Dot Sape Example faithful # Drawing Scatter Plot plot(faithful$eruptions, faithful$waiting, col = "chocolate", pch = 8, main = "R Scatter Plot", xlab = "Eruptions", ylab = "Waiting", las = 1, xlim = c(1.5, 5.5), ylim = c(40, 100)) |

**OUTPUT**

## Adding Linear Progression to Scatter Plot in R

In this example we will show you, How to find the linear progression of two data sets and add it to scatter plot using * abline* function.

**R CODE**

1 2 3 4 5 6 7 8 9 |
# R Scatter Plot - Adding Linear Progression Example faithful # Drawing Scatter Plot plot(faithful$eruptions, faithful$waiting, col = "chocolate", pch = 8, main = "R Scatter Plot", xlab = "Eruptions", ylab = "Waiting", las = 1) #Linear Progression abline(lm(faithful$waiting~faithful$eruptions), col = "red", lwd = 3) |

**OUTPUT**

**ANALYSIS**

Following statement will create a table with records of sales amount, and color. Here, column values are unique colors and row values are unique sales amount

1 |
count <- table(employee$SalesAmount, employee$Color) |

Next, we are adding a line, and the line represents the linear progression.

1 |
abline(lm(faithful$waiting~faithful$eruptions), col = "red", lwd = 3) |

**TIP:** lwd argument is used to change the width of the line

Thank You for Visiting Our Blog

## Share your Feedback, or Code!!