The Wayback Machine - https://web.archive.org/web/20240712014225/https://www.geeksforgeeks.org/how-to-plot-categorical-data-in-r/
Open In App

How to Plot Categorical Data in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

geom_bar()

Example: Plotting bar graph of categorical data

R




library(ggplot2)
  
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))
  
ggplot(data, aes(x=x)) + geom_bar()


Output:

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.

Syntax:

geom_box()

Example: Plotting boxplot of categorical data

R




library(ggplot2)
  
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))
  
ggplot(data, aes(x, y=a)) + geom_boxplot()


Output:

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.

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:



Similar Reads

Set Aspect Ratio of Scatter Plot and Bar Plot in R Programming - Using asp in plot() Function
asp is a parameter of the plot() function in R Language is used to set aspect ratio of plots (Scatterplot and Barplot). Aspect ratio is defined as proportional relationship between width and height of the plot axes. Syntax: plot(x, y, asp ) Parameters: x, y: Coordinates of x and y axis asp: Aspect ratio Example 1: # Set seed for reproducibility set
1 min read
Plot Paired dot plot and box plot on same graph in R
R Programming Language is used for statistical computing and graphics. R was first developed at the University of Auckland by two professors Ross Ihanka and Robert Gentleman Dot Plot The dot plot is a graphical representation of how one attribute varies with respect to another attribute. On the x-axis, we usually plot the attribute with respect to
7 min read
How to Assign Colors to Categorical Variable in ggplot2 Plot in R ?
In this article, we will see how to assign colors to categorical Variables in the ggplot2 plot in R Programming language. Note: Here we are using a scatter plot, the same can be applied to any other graph. Dataset in use: YearPointsUsers1201130user12201220user23201315user34201435user45201550user5 To Create an R plot, we use ggplot() function and to
2 min read
Categorical bubble plot in R
A Scatter plot is a graphical representation of two numeric variables related to each other based on the premise of Cartesian coordinate system where a point or dot is plotted at the intersection of the imaginary vertical and horizontal lines extending from the values of the X and Y axes respectively. One of the variables is represented along the X
6 min read
How to create a frequency table for categorical data in R ?
In this article, we will see how to create a frequency table for categorical data in R Programming Language. Method 1 : Using table() method Tables in R are used for better organizing and summarizing the categorical variables. The table() method takes the cross-classifying factors belonging in a vector to build a contingency table of the counts at
5 min read
Encoding Categorical Data in R
Encoding Categorical Data in R The categorical variables are very often found in data while conducting data analysis and ML(machine learning). The Data which can be classified into categories or groups, such as colors or job titles is generally called as categorical data. The categorical variables must be encoded into numerical values in order to b
6 min read
Categorical Data Descriptive Statistics in R
Categorical data, representing non-measurable attributes, requires specialized analysis. This article explores descriptive statistics and visualization techniques in R Programming Language for categorical data, focusing on frequencies, proportions, bar charts, pie charts, frequency tables, and contingency tables. Categorical Data Categorical data i
12 min read
Plot Only One Variable in ggplot2 Plot in R
In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot &amp; nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firstly the user needs to install and import the ggplo
5 min read
How to Create Categorical Variables in R?
In this article, we will learn how to create categorical variables in the R Programming language. In statistics, variables can be divided into two categories, i.e., categorical variables and quantitative variables. The variables which consist of numerical quantifiable values are known as quantitative variables and a categorical variable is a variab
4 min read
Regression with Categorical Variables in R Programming
Regression is a multi-step process for estimating the relationships between a dependent variable and one or more independent variables also known as predictors or covariates. Regression analysis is mainly used for two conceptually distinct purposes: for prediction and forecasting, where its use has substantial overlap with the field of machine lear
7 min read
three90RightbarBannerImg