Scatter Plots
Explore how to create and customize scatter plots in R using the base plot() function and ggplot2's geom_point(). Understand how to visualize relationships, identify trends, correlations, and outliers between two variables. Gain skills to configure plot aesthetics such as color, size, shape, and labels for clearer data interpretation.
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.
- Line 3: The data in the
mpgcolumn is reflected on thexaxis, anddispis reflected on theyaxis.
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 takes string values, either color names or hexadecimal codes. -
lwd: This changes the ...