Using ggplot2 and ggmosaic, I am generating a facet_grid of mosaic plots using R’s built-in HairEyeColor dataset. And trying to add labels using the geom_mosaic_text argument. But adding a “color=” argument to geom_mosaic_text() has no effect. Am I doing something wrong? Or is there another way to do this?
My code::
library(ggplot2)
library(ggmosaic)
HECdf <-as.data.frame(HairEyeColor) # returns HEC data table to data frame
my.plot <- ggplot(data = HECdf) +
geom_mosaic(aes(x = product(Eye, Hair), fill = Eye, weight = Freq), show.legend = FALSE) +
theme_mosaic() + # specific theme for mosaic plot
labs(title = 'Hair and Eye Color by Sex', # adding title, caption
caption = 'Source: HairEyeColor dataset (R)') +
theme(axis.ticks.x = element_blank(), # removing ticks in x and y axes
axis.ticks.y = element_blank(),
axis.title.x = element_text(color = 'black', face = 'bold'),
axis.title.y = element_text(color = 'black', vjust = 3, face = 'bold'),
axis.text.x = element_text(color = 'black', angle = 45, hjust = 1),
axis.text.y = element_text(color = 'black'),
plot.title = element_text(hjust = 0.5, vjust = 2, face = 'bold'), #modifying title
plot.caption = element_text(hjust = 1), # modifying caption
plot.margin = margin(t = 1, # Top margin
r = 1, # Right margin
b = 1, # Bottom margin
l = 1, # Left margin
unit = "cm")) + # adding margin to plot
scale_fill_manual(values = c("#8B4513", "dodgerblue2", "darkgoldenrod", "#1e8449")) +
facet_grid( ~ factor(Sex, levels = c('Female', 'Male')), # manually adjusting order of facets
scales = "free_x") # adjusting axis label positions per facet plot
my.plot
my.plot + geom_mosaic_text(aes(x = product(Eye, Hair), weight = Freq, label = after_stat(.wt), color = "white")) #adding labels to plot
The geom_mosaic_text() function positions the labels beautifully, but the arguments to alter the color or font size have no effect. What I get is this:
But what I want is this:
I’ve tried repositioning the “color=” argument within the aesthetic of geom_mosaic_text() but to no effect.
For a look at the data, the output of dput(HECdf) is:
structure(list(Hair = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("Black", "Brown",
"Red", "Blond"), class = "factor"), Eye = structure(c(1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), levels = c("Brown",
"Blue", "Hazel", "Green"), class = "factor"), Sex = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Male",
"Female"), class = "factor"), Freq = c(32, 53, 10, 3, 11, 50,
10, 30, 10, 25, 7, 5, 3, 15, 7, 8, 36, 66, 16, 4, 9, 34, 7, 64,
5, 29, 7, 5, 2, 14, 7, 8)), class = "data.frame", row.names = c(NA,
-32L))
Details of my setup:
Rstudio version: 2024.04.1+764
R version: 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.5
packageVersion("plotly"): ‘4.10.4’
packageVersion("ggplot2"): ‘3.5.1’
packageVersion("ggmosaic"): ‘0.3.3’
inter_naut is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.