Visualizing Clusters

library(palmerpenguins)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(magrittr)

Visualizing clusters with a convex hull

penguins %<>% filter_at(vars(bill_length_mm, body_mass_g), all_vars(!is.na(.)))

penguin_hull <- penguins %>%
  group_by(species) %>%
  slice(chull(bill_length_mm, body_mass_g))

ggplot(data = penguins,
       aes(x = bill_length_mm,
           y = body_mass_g,
           color = species,
           labels = species)) +
  geom_point(size = 2) +
  geom_polygon(
    data = penguin_hull,
    aes(x = bill_length_mm,
        y = body_mass_g,
        fill = species),
    alpha = 0.5,
    show.legend = FALSE
  ) +
  xlab("Bill Length (mm)") +
  ylab("Body Mass (g)") +
  theme_minimal() +
  theme(legend.position = "bottom")