Easy Tutorial
❮ R Input Json File R Line_Charts ❯

R Plotting - Scatter Plot

A scatter plot displays all data points as dots on a rectangular coordinate system to show the degree of correlation between variables. The position of each dot is determined by the values of the variables, with each dot corresponding to an X and Y coordinate.

A scatter plot can be created using the plot() function with the following syntax:

plot(x, y, type="p", main, xlab, ylab, xlim, ylim, axes)

Possible values for the type parameter:

Creating a simple line plot:

Example

x <- c(10, 40)
y <- c(20, 60)
# Generate png image
png(file = "runnob-test-plot2.png")

plot(x, y, "l")

Creating a simple line plot with type using "o" parameter, drawing both points and lines with lines passing through points:

Example

x <- c(10, 40)
y <- c(20, 60)
# Generate png image
png(file = "runnob-test-plot.png")

plot(x, y, "o")

Next, we will use the built-in dataset mtcars in R for testing.

We use the wt and mpg columns from the mtcars dataset:

Example

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

Output:

wt  mpg
Mazda RX4         2.620 21.0
Mazda RX4 Wag     2.875 21.0
Datsun 710        2.320 22.8
Hornet 4 Drive    3.215 21.4
Hornet Sportabout 3.440 18.7
Valiant           3.460 18.1

Then we use the above data to generate a scatter plot:

Example

# Data
input <- mtcars[,c('wt','mpg')]

# Generate png image
png(file = "scatterplot.png")

# Set x-axis range from 2.5 to 5, y-axis range from 15 to 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"
)

Scatter Plot Matrix

A scatter plot matrix is a method that uses pairwise scatter plots. It can be seen as a large square matrix where each non-diagonal element is a scatter plot of the variables corresponding to the row and column. The diagonal elements contain the variable names, allowing a clear view of the pairwise correlations between multiple variables.

A scatter plot matrix plots pairwise scatter plots for each numeric variable in the dataset.

In R, a scatter plot matrix can be created using the following function:

pairs(formula, data)

Parameters:

Example

# Output image
png(file = "scatterplot_matrices.png")

# Plot matrix for 4 variables, 12 plots

pairs(~wt+mpg+disp+cyl, data = mtcars, main = "Scatterplot Matrix")
❮ R Input Json File R Line_Charts ❯