Problems with creating Mosaicplots in R
07:47 19 Nov 2025
library(reshape2)
library(rvg)
library(officer)

library(ggmosaic)

library(ggrepel)


x <- matrix(c(212, 256, 144, 707), 2, 2)


dimnames(x) <- list(Measurement_1 = c("Time1_yes", "Time1_no"),
                    Measurement_2 = c("Time2_yes", "Time2_no"))

df <- reshape2::melt(x)

p <- ggplot(df) + 
  geom_mosaic(aes(weight = value, 
                  x = product(Measurement_1, Measurement_2)),
              fill = c("#6b5dd5", "grey80", "grey80", "#6b5dd5")) + 
  labs(x = "Measurement_2", y = "Measurement_1")

df2 <- ggplot_build(p)$data[[1]]

df2$pr <- round(100*df2$.wt/sum(df2$.wt), 2)

df2$lab <- paste0(df2$.wt, "; ", df2$pr, "%")

p + geom_label(data = df2, 
               aes(x = (xmin+xmax)/2, y = (ymin+ymax)/2, label = lab))

I am always getting this error.

Error in make_title(..., self = self) : 
  unused arguments (list(), "Measurement_2")

I have tried to update the versions of the packages but it is still not working. The code should give me this:enter image description here. Another question: How can I remove the grey grid in the background? Is there anyway to remove the box where the counts and percentages are if I needed to and only have the counts and percentages on the coloured cells?

r