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()
- 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
Stack 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 stacked bar plot.
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.
Please Login to comment...