Scatter Plot in R

Scatter plot is a widely used visualization technique to explore the relationship between two variables. In this article, we will discuss how to create scatter plots in R and some of its variations.

Basic Scatter Plot

The basic syntax for creating a scatter plot in R using the scatterplot function from the GGally package is as follows:

library(GGally)

data(mtcars)
ggpairs(~mpg + wt + disp, data = mtcars)

This will create a scatter plot matrix showing the relationship between each pair of variables in the dataset.

Customizing Scatter Plot

You can customize the appearance of your scatter plot by using various arguments available in the scatterplot function. For example, you can disable boxplots and grid or add ellipses to the plot:

library(GGally)

data(mtcars)
ggpairs(~mpg + wt + disp, data = mtcars,
 boxplots = "", 
 grid = FALSE, 
 ellipse = TRUE)

There are many more arguments you can use to customize your scatter plot, so be sure to check out the help page for ggpairs using ?ggpairs.

Scatter Plot Matrix

When dealing with multiple variables, it is often useful to create a scatter plot matrix showing the relationship between each pair of variables. This can be done using the pairs function:

library(stats)

data(mtcars)
pairs(~disp + wt + mpg + hp, data = mtcars)

This will create a matrix of scatter plots showing the relationship between each pair of variables in the dataset.

Coloring Groups

If your dataset contains categorical variables, you can color the groups using the col argument:

library(stats)

data(mtcars)
pairs(~disp + wt + mpg + hp, col = factor(mtcars$am), pch = 19, data = mtcars)

This will color the groups according to the values of the categorical variable.

Alternative Methods

There are several alternative methods you can use to create scatter plots in R. For example, you can use the scatterplotMatrix function from the car package:

library(car)

data(mtcars)
scatterplotMatrix(~disp + wt + mpg + hp, data = mtcars)

This will create a scatter plot matrix with kernel density estimates in the diagonal.

Scatter Plot in ggplot2

Another way to create scatter plots in R is using the ggplot2 package. This package provides a powerful and flexible system for creating a wide variety of plots, including scatter plots:

library(ggplot2)

data(mtcars)
ggplot(mtcars, aes(x = mpg, y = wt)) +
 geom_point(aes(color = factor(am))) + 
 theme_classic()

This will create a scatter plot showing the relationship between mpg and wt, with different colors for each group.

3D Scatter Plot

Finally, if you want to create a 3D scatter plot in R, you can use the scatterplot3d function from the scatterplot3d package:

library(scatterplot3d)

set.seed(2)
x <- rnorm(1000)
y <- rnorm(1000)
z <- rnorm(1000)

scatterplot3d(x, y, z, pch = 19, color = "blue")

This will create a static 3D scatter plot showing the relationship between x, y, and z. You can also use the plot3d function from the rgl package to create an interactive 3D visualization:

library(rgl)

plot3d(x, y, z,
 type = "s",
 radius = 0.1,
 col = "lightblue")

This will allow you to rotate, zoom in and out the scattergram, which can be very useful when looking for patterns in three-dimensional data.

I hope this article has been helpful in showing you how to create scatter plots in R using various libraries and techniques. Happy plotting!