Add percentages labels on top of each square in mosaic plot
12:14 08 Sep 2025

I'm using the penguins dataset from {palmerpenguins} package. I provided my code so far and it runs but the percentages aren't showing up. I want to add percentage labels on top of each square in mosaic plot to look like the graph below.

enter image description here

I tried the code below expecting a mosaic plot with percentage labels on each box, but it just gave me an unlabeled plot.

enter image description here

#make labels 
#find all percentages first of each box 
library(palmerpenguins)
library(dplyr)
pen_perc <- penguins %>%
  count(island, sex) %>%
  group_by(island) %>%
  mutate(percent = n / sum(n))

pen_perc

library(ggplot2)
library(ggmosaic)
library(scales)

ggplot(data = penguins) +
  geom_mosaic(aes(x = product(sex, island), fill = island, weight = 1)) +
  geom_text(
    aes(
      x = after_stat(xcenter),
      y = after_stat(ycenter),
      label = after_stat(percent * 100)  # percent in the stat
    ),
    stat = "mosaic",
    format = scales::percent_format(accuracy = 0.1)
  )

r ggplot2 mosaic-plot r-mosaic ggmosaic