Open In App

Stacked bar plot Using Plotly package in R

 In general, the bar plots are used to plot the categorical data. The stacked bar plot is a type of bar plot which is used to visualize the data effectively in the same bar by plotting them in a stacked manner. These are mostly used when one wants to summarize similar kinds of data by plotting a single graph. In R Programming the stacked bar plots can be plotted using the plot_ly() function of the plotly package as follows…

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 the below commands

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

Stack bar plot for two columns 

In this, we will be creating a data frame that contains 2 columns and then we will be using the Plotly module to plot the stacked bar plot.






# 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 stacked 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 = 'stack',title="Placement Statistics")
fig

Output:

Stacked bar plot Using Plotly package in R

Finally, the plot is stored in the fig object, which can be shown by typing fig in the R console.
 

Stack bar plot for more than two columns

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




# Creation of sample data frame (Student 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 stacked 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 = 'stack',title="Student Statistics")
 
fig

Output:

Stacked bar plot Using Plotly package in R

Horizontal Stack bar plot for more than two columns

Now we can try to plot a stack bar plot in the horizontal direction.




# Creation of sample data frame (Student 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 horizontal stacked bar plot using plot_ly
fig <- plotly::plot_ly(data, y = ~Languages, x = ~Batch_2020,
                       type = 'bar', name = '2020', orientation = 'h') %>%
  add_trace(y = ~Languages, x = ~Batch_2021, name = '2021', orientation = 'h') %>%
  add_trace(y = ~Languages, x = ~Batch_2022, name = '2022', orientation = 'h') %>%
  layout(
    xaxis = list(title = 'Students Count'),
    barmode = 'stack',
    title = "Student Statistics",
    yaxis = list(title = 'Languages'),
    legend = list(title = 'Batches', orientation = 'v',
                  xanchor = 'center', x = 0.5, y = -0.2)
  )
 
fig

Output:

Horizontal Stacked bar plot Using Plotly package in R


Article Tags :