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.
Method 1: Create a bar plot of the categorical data
In this approach to create a bar plot of the categorical data, the user has to first install and import the ggplot2 package in the working R console, here the ggplot2 package is responsible to plot the ggplot2 barplot and providing various functionalities, then the user needs to call the geom_bar() function with the categorical data and then this will be returning a ggplot2 bar plot to the user containing the provided categories and their corresponding frequencies in the R programming language.
geom_bar() function is used to make the height of the bar proportional to the number of cases in each group.
Example: Plotting bar graph of categorical data
Method 2: Create Boxplots by Group of categorical data
In this method to create the boxplot by a group of the given categorical data, the user needs to install and import the ggplot2 package to provide its functionalities and then the user simply needs to call the geom_box() function with the given data to plot a ggplot2 boxplot by the group in the R programming language.
Example: Plotting boxplot of categorical data
Method 3: 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.
- x: a contingency table in array form, with optional category labels specified
Example: Plotting mosaic plot of categorical data
Please Login to comment...