4 Plots In R
- R Tutorial
- R Data Interfaces
- R Charts & Graphs
- R Statistics Examples
R makes it easy to combine multiple plots into one overall graph, using either the par( ) or layout( ) function. With the par( ) function, you can include the option mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. Mfcol=c(nrows, ncols) fills in the matrix by columns. # 4 figures arranged in 2 rows. To see more of the R is Not So Hard! Tutorial series, visit our R Resource page. About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.
- R Useful Resources
- Selected Reading
Scatterplots 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.
4 Diagnostic Plots In R
The simple scatterplot is created using the plot() function.
Plot 4 Lines In R
Syntax
The basic syntax for creating scatterplot in R 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
We use the data set 'mtcars' available in the R environment to create a basic scatterplot. Let's use the columns 'wt' and 'mpg' in mtcars.
When we execute the above code, it produces the following result −
Creating the Scatterplot
The below script will create a scatterplot graph for the relation between wt(weight) and mpg(miles per gallon).
When we execute the above code, it produces the following result −
Scatterplot Matrices
4 Plots In R
When we have more than two variables and we want to find the correlation between one variable versus the remaining ones we use scatterplot matrix. We use pairs() function to create matrices of scatterplots.
Syntax
The basic syntax for creating scatterplot matrices in R 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 scatterplot is plotted for each pair.
4 Quadrant Plot In R
When the above code is executed we get the following output.