Scatter plots show many points plotted in the Cartesian plane. Each point represents the values of two variables. One variable is chosen in the horizontal axis and another in the vertical axis. The simple R scatter plot is created using the plot() function.
Syntax
The basic syntax for creating R scatter plot is :
Following is the description of the parameters used:
- x is the data set whose values are the horizontal coordinates.
- y is the data set whose values are the vertical coordinates.
- main is the tile of the graph.
- xlab is the label in the horizontal axis.
- ylab is the label in the vertical axis.
- xlim is the limits of the values of x used for plotting.
- ylim is the limits of the values of y used for plotting.
- axes indicates whether both axes should be drawn on the plot.
Example R Scatter Plot
We use the data set “mtcars” available in the R environment to create a basic scatter plot. Let’s use the columns “wt” and “mpg” in mtcars.
# scatter plot in R input <- mtcars[,c('wt','mpg')] # Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30. plot(x=input$wt,y=input$mpg, xlab="Weight", ylab="Mileage", xlim=c(2.5,5), ylim=c(15,30), main="Weight vs Mileage")
When we execute the above code, it produces the following result:
R Scatter plot Matrices
When we have more than two variables and we want to find the correlation between one variable versus the remaining ones we use scatter plot matrix. We use pairs() function to create matrices of scatter plot in R
Syntax
The basic syntax for creating R scatter plot matrices is :
Following is the description of the parameters used:
- formula represents the series of variables used in pairs.
- data represents the data set from which the variables will be taken.
Example
Each variable is paired up with each of the remaining variable. A scatter plot is plotted for each pair
# scatter plot matrix in R - 4 variables is plotted against each other. pairs(~wt mpg disp cyl,data=mtcars,main="Scatterplot Matrix")
four variables of mtcars data set is plotted against each other. And the output will be