Open In App

Unstack bar plots in R

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In general, the bar plots are used to represent the categorical data. The stacked bar plots are the combined plots where each color represents one group of data. 

The unstacked bar plot is the one where each group of categorical data is represented using different bars and colors instead of stacking upon one on one. In R Programming this type of plot can be implemented using the plot_ly() function present in the plotly package.

Syntax: plot_ly(df,type,marker,labels,values) %>% layout() %>% add_trace()

Where,

  • df – data frame
  • type – used to specify the type of plot we want to visualize
  • marker – used to mark the plot with different colors using color attribute
  • labels – names of categorical variables in the dataset
  • values – values of the columns in the dataset that we want to plot are specified here (optional)
  • layout() –  this function is used to change the layout as required (like assigning a title to the plot)
  • add_trace() – this function is used to append similar new traces to existing dimension

Let’s install and load the plotly library with the help of below commands

install.packages("plotly")
library(plotly)

Unstack bar plot for two columns 

In this, we will be creating a data frame which contains 2 columns and then we will be using plotly module to plot the unstacked bar plot.

R




# Creation of sample data frame (Placement Statistics)
data <- data.frame(
  Branches<-c('CSE','ECE','MECH','EEE','CIVIL'),
  Placements_2021 <-c(99,98,89,90,75),
  Placements_2022 <- c(95,94,93,85,77))
 
# Plotting the unstacked bar plot using plot_ly
fig <- plotly::plot_ly(data,x = ~Branches,y=~Placements_2022,
                       type = 'bar', name = '2022') %>%
       add_trace(y=~Placements_2021,name = '2021') %>%
       layout(yaxis = list(title = 'Placements_Count'),
              barmode = 'unstack',title="Placement Statistics")
 
fig


Output:

Output

 

Unstack bar plot for more than two columns

We can also implement the unstack bar plotting for more than 2 columns. Let’s see the code for it.

R




# Creation of sample data frame (Placement Statistics)
data <- data.frame(
  Languages<-c('Python','R','C++','Java','Ruby'),
  Batch_2020 <-c(199,398,319,910,735),
  Batch_2021 <- c(952,942,933,851,771),
  Batch_2022 <- c(1022,982,983,811,721))
 
# Plotting the unstacked bar plot using plot_ly
fig <- plotly::plot_ly(data,x = ~Languages,y=~Batch_2020,
                       type = 'bar', name = '2020') %>%
       add_trace(y=~Batch_2021,name = '2021') %>%  
        add_trace(y=~Batch_2022,name = '2022') %>%
       layout(yaxis = list(title = 'Students_Count'),
              barmode = 'unstack',title="Student Statistics")
 
fig


Output:

Output

 



Similar Reads

Multiple Line Plots or Time Series Plots with ggplot2 in R
In this article, we will discuss how to plot Multiple Line Plots or Time Series Plots with the ggplot2 package in the R Programming Language. We can create a line plot using the geom_line() function of the ggplot2 package. Syntax: ggplot( df, aes( x, y ) ) + geom_line() where, df: determines the dataframe usedx and y: determines the axis variables
2 min read
How to Make Grouped Bar Plot with Same Bar Width in R
In this article, we will discuss How to Make Grouped Bar Plot with the Same Bar Width in R Programming Language. Method 1 : Using position_dodge2(preserve = “single”) The geom_col() method can be used to add positions to the graph. Dodging preserves the vertical position of an geom while adjusting the horizontal position. The position_dodge2 method
2 min read
Creating Interactive Plots with R and Highcharts
The R Programming language is widely used for statistics, data visualization and data analysis, etc. Using the Highchart library data is graphically represented in the software. Not only meaning but Interactive charts are also prepared. Types of charts:Column ChartBar ChartPie ChartScatter PlotExamples: Creating Interactive Charts for the following
3 min read
Density Plots Using Lattice Package in R
The density plots are mainly used to visualize the distribution of continuous numeric variables. The densityplot() uses kernel density probability estimate to calculate the density probability of numeric variables. In R Programming, the density plot can be plotted using the densityplot() function which is present in the lattice package. Syntax: den
2 min read
Scatter plots in R Language
A scatter plot is a set of dotted points representing individual data pieces on the horizontal and vertical axis. In a graph in which the values of two variables are plotted along the X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. R - Scatter plots We can create a scatter plot in R Programming Language us
4 min read
Creating 3D Plots in R Programming - persp() Function
3D plot in R Language is used to add title, change viewing direction, and add color and shade to the plot. The persp() function which is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane. persp() is defines as a generic function. Moreover, it can be used to superimpose addition
3 min read
R - Stem and Leaf Plots
Stem and Leaf plot is a technique of displaying the frequencies with which some classes of values may occur. It is basically a method of representing the quantitative data in the graphical format. The stem and leaf plot retains the original data item up to two significant figures unlike histogram. The data is put in order which eases the move to no
6 min read
Adding Legend to Multiple Line Plots with ggplot in R
In this article, we are going to see how can we add a legend to multiple line plots with ggplot in the R programming language. For a plot that contains more than one line plot, a legend is created by default if the col attribute is used. All the changes made in the appearance of the line plots will also reflect in the legend. Syntax: ggplot(df, aes
2 min read
Draw unbalanced grid of ggplot2 Plots in R
In this article, we are going to see how to Draw an Unbalanced Grid of ggplot2 plots in R Programming Language. We use mainly grid.arrange() and arrangeGrob() functions. Following is the brief information of these two functions. grid.arrange(): It will calculate a different number of rows and columns to organize the plots and for forming nested lay
4 min read
Combine two ggplot2 plots from different DataFrame in R
In this article, we are going to learn how to Combine two ggplot2 plots from different DataFrame in R Programming Language. Here in this article we are using a scatter plot, but it can be applied to any other plot. Let us first individually draw two ggplot2 Scatter Plots by different DataFrames then we will see how to combine them i.e how draw both
2 min read