Create Mosaic Plot of categorical data
In this method, the user has to simply call the mosaicplot() function with the data passed into this function as the parameter to get the resulting plot as the mosaic plot of the given categorical data in the R language.
mosaicplot() function is used to plot a mosaic on the current graphics device.
Syntax:
mosaicplot(x,)
Parameters:
- x: a contingency table in array form, with optional category labels specified
Example: Plotting mosaic plot of categorical data
R
data < - data.frame (x= c ( 'M' , 'F' , 'M' , 'F' , 'M' , 'F' , 'M' , 'F' , 'M' , 'F' , 'M' , 'F' , 'M' , 'M' , 'M' ), y= c ( 'B' , 'G' , 'B' , 'B' , 'G' , 'G' , 'B' , 'G' , 'G' , 'B' , 'G' , 'G' , 'B' , 'G' , 'G' ), a= c (8, 6, 6, 1, 2, 3, 7, 4, 4, 2, 5, 8, 1, 3, 2), b= c (5, 7, 7, 4, 5, 6, 7, 8, 8, 6, 9, 4, 1, 8, 1)) count < - table (data$b, data$y) mosaicplot (count) |
Output:
How to Plot Categorical Data in R?
In this article, we will be looking at different plots for the categorical data in the R programming language.
Categorical Data is a variable that can take on one of a limited, and usually fixed, a number of possible values, assigning each individual or other unit of observation to a particular group or nominal category on the basis of some qualitative property.