Adding data value in geom_mosaic and percentages as well

Hi, I would like to have both, values and percentages in geom_mosaic.
here is the code:

library(tidyverse)
librarry(ggmosaic)

ddf1 <- structure(list(
  marit = structure(c(
    3L, 3L, 3L, 3L, 1L, 2L, 4L,
    1L, 4L, 4L, 3L, 3L, 3L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 4L, 3L,
    1L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 2L, 4L, 3L, 4L, 4L, 3L,
    2L, 3L, 4L, 4L, 1L, 2L, 3L, 3L, 1L, 3L, 4L, 4L, 3L, 4L, 3L, 4L,
    1L, 3L, 3L, 3L, 4L, 2L, 3L, 4L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 3L,
    3L, 3L, 2L, 3L, 4L, 3L, 3L, 3L, 3L, 2L, 3L, 4L, 3L, 3L, 3L, 4L,
    1L, 3L, 3L, 4L, 4L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 3L, 4L,
    2L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 3L, 1L,
    4L, 4L, 3L, 3L, 4L, 2L, 3L, 3L, 1L, 4L, 3L, 3L, 1L, 3L, 3L, 2L,
    3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 4L, 3L, 3L, 4L, 4L,
    3L, 1L, 2L, 4L, 3L, 3L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 4L,
    3L, 3L, 4L, 1L, 3L, 1L, 3L, 2L, 4L, 3L, 3L, 4L, 4L, 3L, 3L, 4L,
    1L, 1L, 1L, 4L, 3L, 3L, 3L, 3L, 4L, 2L, 3L, 3L, 3L, 4L, 4L, 4L,
    4L, 3L, 3L, 3L, 4L, 4L, 1L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
    3L, 3L, 4L, 3L, 4L, 3L, 3L, 3L, 2L, 4L, 2L, 4L, 2L, 4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 4L, 2L, 3L, 3L,
    3L, 4L, 3L, 2L, 1L, 4L, 5L, 2L, 2L, 4L, 4L, 3L, 2L, 4L, 1L,
    2L, 1L, 2L, 2L, 5L, 4L, 1L, 5L, 2L, 1L, 4L, 2L, 4L, 1L, 3L,
    2L, 2L, 3L, 4L, 4L, 2L, 3L, 4L, 3L, 2L, 5L, 3L, 2L, 1L, 2L,
    4L, 3L, 2L, 2L, 1L, 1L, 1L, 4L, 3L, 2L, 2L, 3L, 4L, 4L, 2L,
    4L, 2L, 3L, 3L, 4L, 2L, 2L, 5L, 3L, 2L, 2L, 3L, 4L, 2L, 4L,
    3L, 3L, 2L, 2L, 4L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 1L, 5L, 3L,
    5L, 2L, 4L, 5L, 1L, 3L, 4L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L,
    3L, 4L, 3L, 3L, 5L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 3L, 2L, 1L,
    3L, 3L, 3L, 2L, 3L
  ), levels = c("1", "2", "3", "4", "5"), class = "factor")
), class = "data.frame", row.names = c(
  NA,
  -300L
), variable.labels = c(
  name = "", gender = "", marit = "Marital Status",
  educ = "Education Level"
), codepage = 65001L)

ppp <- ggplot(ddf1) + geom_mosaic(aes(x = product(educ), fill = marit))

ppp +  geom_text(data = ggplot_build(ppp)$data[[1]], aes(x = (xmin+xmax)/2, y = (ymin+ymax)/2, label=.wt))

I would like to ask how to add percentages as when I have been trying it gives me an error.

Additionally I want to reverse levels in legend according to marit variable, how do I do it please.
Is it possible to add a second legend for educ variable as well ?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.