Scatter Plot in R Programming

Scatter Plot in R Programming

A scatter plot is a graphical representation of the relationship between two continuous variables. In R, we can create a scatter plot using the plot() function.

Simple Scatter Plot Chart

To create a simple scatter plot chart in R, we need to use the mtcars data set and select the columns wt and mpg.

input <- mtcars[, c('wt', 'mpg')]
print(head(input))

Next, we will create the scatter plot:

plot(x = input$wt, y = input$mpg,
 xlab = "Weight",
 ylab = "Miles per Gallon",
 main = "Scatter Plot of Weight vs. Miles per Gallon")

This code will generate a scatter plot with the weight on the x-axis and the miles per gallon on the y-axis.

Adding More Plots

To add more plots to our graph, we can use the points() function:

plot(x = input$wt, y = input$mpg,
 xlab = "Weight",
 ylab = "Miles per Gallon",
 main = "Scatter Plot of Weight vs. Miles per Gallon")

points(x2, y2)

This code will add another scatter plot to our graph.

Using Different Colors

To differentiate between the two plots, we can use different colors:

plot(x = input$wt, y = input$mpg,
 xlab = "Weight",
 ylab = "Miles per Gallon",
 main = "Scatter Plot of Weight vs. Miles per Gallon")

points(x2, y2, col = "blue")

This code will add the second plot to our graph using blue color.


In this article, we have learned how to create a scatter plot in R and add more plots to our graph. We also discussed how to differentiate between the two plots by using different colors.

Leave a comment