Scatterplots are commonly used to visualize the relationship between two continuous variables. For example, you may be interested in the relationship between body mass and bill length across all islands:
ggplot(data = penguins,
mapping = aes(x = bill_length_mm, y = body_mass_g)) +
geom_point()
The above code first specifies the data
(penguins
). The mapping
arguments define “which variables belong where”. In this case, bill_length_mm
is on the x axis, body_mass_g
on the y. The geom
of geom_point
specifies that this is a scatterplot.
You could add the visual variable of color to show patterns within each island:
ggplot(data = penguins,
mapping = aes(x = bill_length_mm, y = body_mass_g, color = island)) +
geom_point()