Scatter Plots
Explore the features of scatter plots and learn how to create them.
We'll cover the following...
Features
Scatter plots show data points on a chart by crossing at least two features of an element.
Scatter plots show the relationship between two variables, each represented on different axes. We can identify trends, correlations, and outliers by looking at the scatter plots. We do not prefer scatter plots when two datasets are not related.
Scatter plot with plot()
The built-in function plot()
generates scatter plots in R. It takes at least two variables that contain the data to be represented. We define the variables as x
and y
.
# Syntax structure
plot(x = <variable1>, y = <variable2>)
Let’s create a simple scatter plot using the mtcars
dataset.
# We use the `mtcars` dataset in this exercisehead(mtcars) # Preview of the datasetplot(x = mtcars$mpg, y = mtcars$disp) # Define the variables in the function.
- Line 3: The data in the
mpg
column is reflected on thex
axis, anddisp
is reflected on they
axis.
We can state that the data in these columns are negatively correlated because as mpg
increases, disp
decreases.
We can configure the chart using the following arguments:
-
col
: This changes the colors of the points on the chart. This argument ...