Mosaic Plot in R Programming
Mosaic Plots are used to show symmetries for tables that are divided into two or more conditional distributions. Mosaic plots are a great way to visualize hierarchical data. A collection of rectangles represents all the elements to be visualized with the rectangles of different sizes and colors makes a table, but what makes these mosaic charts unique is the arrangement of the elements where there is a hierarchy those elements are collected and labeled together, perhaps even with subcategories. So mosaic plots can be used for plotting categorical data very effectively, with the area of the data showing the relative proportions.
In this article, we will learn how to create a mosaic plot in R programming language. The package that is used for this is vcd.
Method 1: Using mosaic() function
The function used for creating a mosaic plot in R programming language is mosaic().
mosaic(x,shade=NULL,legend=NULL, main = NULL,..)
- x: Here, x is pointing to the variable that holds the dataset/table. We passed our dataset name here.
- shade: shade is a boolean variable, if it is set to be true then we will get a colored plot. Its default value is NULL.
- legend: the legend is a boolean variable, if it is set to be true then we will be able to see legends alongside our mosaic plot. Its default value is NULL.
- main: main is a string variable, here we pass the title of our mosaic plot.
To create a plot, first the package is loaded into space, and the dataset is created. The created dataset is passed to the function.
A mosaic plot can also be drawn with custom features to make it more presentable.