36 Grouped scatter plots
36.1 Description
Grouped scatter-plots display the relationships between two continuous variables across a third categorical variable.
The x
and y
position displays the relationship between the two continuous variables, and color is used to distinguish between the categorical levels.
36.2 Set up
PACKAGES:
Install packages.
show/hide
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
The penguins
data.
show/hide
<- palmerpenguins::penguins
penguins glimpse(penguins)
#> Rows: 344
#> Columns: 8
#> $ species <fct> Adelie, Adelie, Adelie…
#> $ island <fct> Torgersen, Torgersen, …
#> $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, …
#> $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, …
#> $ flipper_length_mm <int> 181, 186, 195, NA, 193…
#> $ body_mass_g <int> 3750, 3800, 3250, NA, …
#> $ sex <fct> male, female, female, …
#> $ year <int> 2007, 2007, 2007, 2007…
36.3 Grammar
CODE:
Create labels with
labs()
Initialize the graph with
ggplot()
and providedata
Map
bill_length_mm
to thex
axisMap
flipper_length_mm
to they
axisMap
species
tocolor
inside thegeom_point()
show/hide
<- labs(
labs_grp_scatter title = "Bill Length vs. Flipper Length",
x = "Bill Length (mm)",
y = "Flipper length (mm)",
color = "Species")
<- penguins |>
ggp2_grp_scatter ggplot(
aes(x = bill_length_mm,
y = flipper_length_mm)) +
geom_point(aes(color = species))
+
ggp2_grp_scatter labs_grp_scatter
GRAPH:
Adjust over-plotting with transparency (alpha
).