Scatter Plot in R

Scatter Plot in R

Scatter plot is a graphical representation that shows the relationship between two or more continuous variables. In R, there are several ways to create scatter plots.

Basic Scatter Plot

The basic scatter plot can be created using the plot() function.

plot(x, y)

This will create a simple scatter plot with no customization.

Customizing the Scatter Plot

To customize the scatter plot, you can use various arguments in the plot() function. For example, you can change the point type and size, add a title, and modify the axis labels.

plot(x, y,
 pch = 19, # Change point type to 19 (filled circle)
 cex = 2, # Increase point size to 2
 main = "Scatter Plot", # Add a title
 xlab = "Variable X", # Modify the X-axis label
 ylab = "Variable Y") # Modify the Y-axis label

Disabling the Legend

If you don't want to display the legend, you can set the legend argument to FALSE.

plot(x, y,
 pch = 19,
 cex = 2,
 main = "Scatter Plot",
 xlab = "Variable X",
 ylab = "Variable Y",
 legend = FALSE)

Adding a Grid

You can add a grid to the scatter plot by setting the grid argument to TRUE.

plot(x, y,
 pch = 19,
 cex = 2,
 main = "Scatter Plot",
 xlab = "Variable X",
 ylab = "Variable Y",
 grid = TRUE)

Adding an Ellipse

You can add an ellipse to the scatter plot by setting the ellipse argument to TRUE.

plot(x, y,
 pch = 19,
 cex = 2,
 main = "Scatter Plot",
 xlab = "Variable X",
 ylab = "Variable Y",
 ellipse = TRUE)

Scatter Plot Matrix

When dealing with multiple variables, it's common to create a scatter plot matrix using the pairs() function.

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

This will create a matrix of scatter plots that show the relationship between each pair of variables.

Scatter Plot Matrix with Customization

You can customize the scatter plot matrix by setting various arguments in the pairs() function. For example, you can change the point type and size, add a title, and modify the axis labels.

pairs(~disp + wt + mpg + hp,
 data = mtcars,
 pch = 19,
 cex = 2,
 main = "Scatter Plot Matrix",
 xlab = "Variable X",
 ylab = "Variable Y")

ggplot2 Scatter Plot

You can create a scatter plot using the ggplot2 library.

library(ggplot2)

my_df <- data.frame(x = x, y = y, group = group)

ggplot(my_df, aes(x = x, y = y)) +
 geom_point(aes(colour = group)) + # Points and color by group
 scale_color_discrete("Groups") + # Change legend title
 xlab("Variable X") + # X-axis label
 ylab("Variable Y") # Y-axis label

3D Scatter Plot

You can create a 3D scatter plot using the scatterplot3d and rgl libraries.

library(scatterplot3d)
library(rgl)

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

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

plot3d(x, y, z,
 type = "s", # Type of the plot
 radius = 0.1, # Radius of the observations
 col = "lightblue", # Color of the observations
 xlab = "X axis lab", # Label of the X axis
 ylab = "Y axis lab", # Label of the Y axis
 zlab = "Z axis lab") # Label of the Z axis

These are some examples of how to create scatter plots in R. You can customize your scatter plot by adding various arguments and using different libraries.

Leave a comment