Know your data
Visualize your data – categorical variables
7. Draw the mosaic chart 1
Work with your chart 2
Check yourself 2

Instruction

Ok. We now know what data we need and what we should achieve after the first step of our mosaic plot construction project. In reality, when you use ggplot2 you don't have to prepare data for a mosaic plot – a new geometry called geom_mosaic will take care of it. Nevertheless, knowing what values are encoded in the mosaic plot is crucial if you want to understand this complicated chart.

To create this first stage of the mosaic plot using R, you have to add geom_mosaic to the main ggplot function:

ggplot(data = dataset) + geom_mosaic(aes(x = product(variable_x)))

Note that you specify which variable will be on the horizontal axis by the argument product(variable_x). Obviously, the variable name will replace the variable_x placeholder text.

Exercise

Start your mosaic plot using the commands discussed above. Use alcohol_wealth2 for the dataset and wealth_index_cat as variable_x.

When you're done, press the Run and Check Code button.

Stuck? Here's a hint!

You should write:

ggplot(data = alcohol_wealth2) + geom_mosaic(aes(x = product(wealth_index_cat)))