Create a Basic Mosaic Plot
A mosaic plot represents the cells of a contingency table as proportional rectangles.
R Graphics · Mosaic Plot
Build basic, shaded, labeled, multiway, proportional, and model-based mosaic displays.
Mosaic plots divide a rectangle according to contingency-table frequencies. Tile areas show joint proportions and make associations between categorical variables visible.
A mosaic plot represents the cells of a contingency table as proportional rectangles.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-table(Titanic[,"Class"],Titanic[,"Survived"]);mosaicplot(tab,color=c("#bfdbfe","#2563eb"),main="Class and survival")Residual shading highlights cells that differ from independence expectations.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-xtabs(Freq~Class+Survived,data=as.data.frame(Titanic));mosaicplot(tab,shade=TRUE,main="Departures from independence")Clear labels identify the dimensions and category levels encoded by tile position.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-xtabs(Freq~Sex+Survived,data=as.data.frame(Titanic));mosaicplot(tab,color=c("#dbeafe","#1d4ed8"),xlab="Sex",ylab="Survival")A multiway mosaic recursively subdivides tiles to represent another categorical variable.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-xtabs(Freq~Class+Sex+Survived,data=as.data.frame(Titanic));mosaicplot(tab,color=TRUE,main="Class, sex, and survival")Reordering table dimensions changes which conditional comparison is visually primary.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-xtabs(Freq~Survived+Class,data=as.data.frame(Titanic));mosaicplot(tab,color=hcl.colors(4,"Blues 3"),main="Survival composition by class")A restrained palette can distinguish outcomes while keeping tile areas central.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.
tab<-xtabs(Freq~Class+Survived,data=as.data.frame(Titanic));mosaicplot(tab,color=c("#fca5a5","#60a5fa"),border="white",main="Titanic survival")