A scatter plot is a graphical representation of the relationship between two variables. In R, you can create a scatter plot using the scatterplot function from the ggplot2 library. The basic syntax is as follows:

scatterplot(y ~ x | group)

This code will create a scatter plot with y-axis on the left and x-axis at the bottom. The points are colored based on the group variable.

You can customize the appearance of the scatter plot by adding additional arguments to the scatterplot function. For example, you can disable the legend by setting the legend argument to FALSE.

scatterplot(x, y,
 boxplots = "", # Disable boxplots
 grid = FALSE, # Disable plot grid
 ellipse = TRUE) # Draw ellipses

You can also add more arguments to customize the scatter plot further. For example, you can change the color of the points or add a title to the plot.

Scatter Plot Matrix in R

When dealing with multiple variables, it is common to create a scatter plot matrix, which is a matrix of scatter plots that shows the correlation between each pair of variables. You can create a scatter plot matrix using the pairs function.

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

This code will create a scatter plot matrix with the variables disp, wt, mpg, and hp. The points are colored based on the am variable.

You can also customize the appearance of the scatter plot matrix by adding additional arguments to the pairs function. For example, you can change the color of the points or add a title to the plot.

Alternative Methods

There are alternative methods to create scatter plots in R. One method is to use the scatterplotMatrix function from the car package.

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

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

Another alternative method is to use the cpairs function from the gclus package.

library(gclus)
data <- mtcars[c(1, 3, 5, 6)] # Some numeric variables
cpairs(data)

This code will create a scatter plot matrix with the variables in the order of their correlation.

Scatter Plot in ggplot2

You can also create a scatter plot using the ggplot2 library. The basic syntax is as follows:

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
 theme(axis.line = element_line(colour = "black", # Changes the default theme
 size = 0.24))

This code will create a scatter plot with the points colored based on the group variable.

3D Scatter Plot

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

library(scatterplot3d)

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

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

This code will create a static 3D scatter plot with the points colored blue.

Alternatively, you can use the plot3d function from the rgl package to create an interactive 3D scatter plot.

library(rgl)

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

This code will create an interactive 3D scatter plot that allows you to rotate, zoom in and out, and change the viewing angle.

I hope this helps! Let me know if you have any questions or need further assistance.