# NOT RUN {
data(Titanic)
titanic <- as.data.frame(Titanic)
titanic$Survived <- factor(titanic$Survived, levels=c("Yes", "No"))
ggplot(data=titanic) +
geom_mosaic(aes(weight=Freq, x=product(Class), fill=Survived))
# good practice: use the 'dependent' variable (or most important variable)
# as fill variable
ggplot(data=titanic) +
geom_mosaic(aes(weight=Freq, x=product(Class, Age), fill=Survived))
ggplot(data=titanic) +
geom_mosaic(aes(weight=Freq, x=product(Class), conds=product(Age), fill=Survived))
ggplot(data=titanic) +
geom_mosaic(aes(weight=Freq, x=product(Survived, Class), fill=Age))
# Just excluded for timing. Examples are included in testing to make sure they work
# }
# NOT RUN {
data(happy, package="productplots")
ggplot(data = happy) + geom_mosaic(aes(x=product(happy)), divider="hbar")
ggplot(data = happy) + geom_mosaic(aes(x=product(happy))) +
coord_flip()
# weighting is important
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(happy)))
ggplot(data = happy) + geom_mosaic(aes(weight=wtssall, x=product(health), fill=happy)) +
theme(axis.text.x=element_text(angle=35))
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(health), fill=happy), na.rm=TRUE)
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(health, sex, degree), fill=happy),
na.rm=TRUE)
# here is where a bit more control over the spacing of the bars is helpful:
# set labels manually:
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(age), fill=happy), na.rm=TRUE, offset=0) +
scale_x_productlist("Age", labels=c(17+1:72))
# thin out labels manually:
labels <- c(17+1:72)
labels[labels %% 5 != 0] <- ""
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(age), fill=happy), na.rm=TRUE, offset=0) +
scale_x_productlist("Age", labels=labels)
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(age), fill=happy, conds = product(sex)),
divider=mosaic("v"), na.rm=TRUE, offset=0.001) +
scale_x_productlist("Age", labels=labels)
# facetting works!!!!
ggplot(data = happy) +
geom_mosaic(aes(weight=wtssall, x=product(age), fill=happy), na.rm=TRUE, offset = 0) +
facet_grid(sex~.) +
scale_x_productlist("Age", labels=labels)
ggplot(data = happy) +
geom_mosaic(aes(weight = wtssall, x = product(happy, finrela, health)),
divider=mosaic("h"))
ggplot(data = happy) +
geom_mosaic(aes(weight = wtssall, x = product(happy, finrela, health)), offset=.005)
# Spine example
ggplot(data = happy) +
geom_mosaic(aes(weight = wtssall, x = product(health), fill = health)) +
facet_grid(happy~.)
# }
# NOT RUN {
# end of don't run
# }
Run the code above in your browser using DataCamp Workspace