ggplot2ggmosaicgeom which allows for facetting and layeringggmosaicggmosaic was created primarily using ggproto and the productplots package
ggproto allows you to extend ggplot2 from within your own packages
ggmosaic began as a geom extension of the rect geomproductplots packagerect geom to plotggplot2 is not capable of handling a variable number of variables
current solution: read in the variables x1 and x2 as x = product(x1, x2)
product function:allows for it to pass check_aesthetics
These limitations also lead to issues with the labeling, but those can be fixed manually.
Aesthetics that can be set:
These values are then sent through productplots functions to create the formula for the desired distribution
Formula: weight ~ fill + x | conds
Example of how the formula is built
These aesthetics set up the formula for the distribution:
Formula: 1 ~ W + X + Y | Z
Because a mosaic plot is constructed hierarchically through alternating spines, the ordering of the variables is very important.
ggplot(data = fly) +
geom_mosaic(aes(x = product(RudeToRecline), fill=RudeToRecline), na.rm=TRUE) +
labs(x="Is it rude recline? ", title='f(RudeToRecline)') ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, RudeToRecline), fill=DoYouRecline), na.rm=TRUE) +
labs(x = "Is it rude recline? ", title='f(DoYouRecline | RudeToRecline) f(RudeToRecline)')ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, RudeToRecline), fill=DoYouRecline, conds=product(Gender)), na.rm=TRUE, divider=mosaic("v")) + labs(x = "Is it rude recline? ", title='f(DoYouRecline, RudeToRecline| Gender)')ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, RudeToRecline), fill=DoYouRecline), na.rm=TRUE) + labs(x = "Is it rude recline? ", title='f(DoYouRecline, RudeToRecline| Gender)') + facet_grid(Gender~.)order1 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(DoYouRecline, RudeToRecline), fill=DoYouRecline), na.rm=TRUE) + labs(x = "Is it rude recline? ", title='f(DoYouRecline | RudeToRecline) f(RudeToRecline)') + theme(plot.title = element_text(size = rel(1)))
order2<- ggplot(data = fly) +
geom_mosaic(aes(x = product(RudeToRecline, DoYouRecline), fill=DoYouRecline), na.rm=TRUE) + labs(x = "" , y = "Is it rude recline? ", title='f(DoYouRecline | RudeToRecline) f(RudeToRecline)') + coord_flip() + theme(plot.title = element_text(size = rel(1)))Arguments unique to geom_mosaic:
Four options available for each partion:
hbar <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq), fill=FlightFreq), divider="hbar", na.rm=TRUE) + labs(x=" ", title='divider = "hbar"')
hspine <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq), fill=FlightFreq), divider="hspine", na.rm=TRUE) + labs(x=" ", title='divider = "hspine"')
vbar <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq), fill=FlightFreq), divider="vbar", na.rm=TRUE) + labs(y=" ", x="", title='divider = "vbar"')
vspine <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq), fill=FlightFreq), divider="vspine", na.rm=TRUE) + labs(y=" ", x="", title='divider = "vspine"') h_mosaic <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=mosaic("h")) +
theme(axis.text.x=element_blank(), legend.position="none") +
labs(x=" ", title='divider= mosaic()')
v_mosaic <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=mosaic("v")) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= mosaic("v")')
doubledecker <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=ddecker()) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= ddecker()')grid_arrange_shared_legend(h_mosaic, v_mosaic, doubledecker, ncol = 3, nrow = 1, position = "right")mosaic4 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=c("vspine", "vspine", "hbar")) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= c("vspine", "vspine", "hbar")')
mosaic5 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=c("hbar", "vspine", "hbar")) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= c("hbar", "vspine", "hbar")')
mosaic6 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=c("hspine", "hspine", "hspine")) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= c("hspine", "hspine", "hspine")')
mosaic7 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Gender, Region), fill=FlightFreq), na.rm=T, divider=c("vspine", "vspine", "vspine")) +
theme(axis.text.x=element_blank()) +
labs(x=" ", title='divider= c("vspine", "vspine", "vspine")')offset: Set the space between the first spine
offset1 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Region), fill=FlightFreq), na.rm=TRUE) + labs(x="Region", y=" ", title=" offset = 0.01")
offset0 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Region), fill=FlightFreq), na.rm=TRUE, offset = 0) + labs(x="Region", y=" ", title=" offset = 0")
offset2 <- ggplot(data = fly) +
geom_mosaic(aes(x = product(FlightFreq, Region), fill=FlightFreq), na.rm=TRUE, offset = 0.02) + labs(x="Region", y=" ", title=" offset = 0.02")