How to draw stacked bars in ggplot2 that show percentages in R ?
Last Updated :
05 Nov, 2021
In this article, we are going to see how to draw stacked bars in ggplot2 that show percentages in R programming language.
The plyr package in R is used to split the data apart, perform operations with it, and then subsequently bring it back together again. It is used to perform data manipulation. This package can be downloaded and installed into the working space using the following command :
install.packages("plyr")
The ddply method in R is used to combine the results into a single data frame after the application of a function on each of the subsets of the data frame.
Syntax: ddply(.data, .variables, .fun = NULL)
Arguments :
- data: The data frame to be used
- variables: variables to split data frame by
- fun: the function to be applied on the data frame
The function to be applied here can be the transform function, which can be used to append or remove or mutate columns in the data frame. It can be used to add more columns in the dataframe. The percentage column can be added to the data frame by calculating the fraction of each component in the dataframe.
The percentage column can then be used to append labels using "%" sign. The column is constructed using the paste0() method which is used to concatenate strings by combining the percentage with the corresponding "%" sign.
paste0(str1, str2)
The ggplot2 package is used for data visualization and depicting the plots. This package can be downloaded and installed into the working space using the following command :
install.packages("ggplot2")
The ggplot method in this package is used to construct various kinds of plots, like scatter plots, boxplots, etc. The plots take as input the data frame to be used and also supply aesthetic mappings using the x and y coordinates. Other arguments can be added by using the color specified by the grouping column.
Syntax: ggplot (data, mapping = aes(x= , y= , fill = ))
Arguments :
- data: The data frame to be used
- mapping: aesthetic mapping supplied by aes() method
The geom_bar() method in this package is used to make the height of the bar proportional to the number of cases in each group. It has the following syntax :
Syntax: geom_bar(position , stat = "identity" )
Arguments :
- position: Position adjustment
The geom_text method can be used to add text to the stacked bars and stack them on top of one another. The label is assigned as the percentage label string computed. The label can be assigned using the label argument and its corresponding position. It can be further customized using the size parameter.
Syntax: geom_text(mapping = NULL, position , size)
Arguments :
- mapping: Aesthetic mappings
- position: The position adjustment to use for overlapping points on this layer
- size: the size of the text added
Example:
R
# importing the required library
library(plyr)
library(ggplot2)
# creating the data frame
data_frame < - data.frame(stringsAsFactors=FALSE,
col1=c(rep(5: 7, each=4)),
col2=c(rep(1: 4, each=3)),
col3=c(1: 12))
# printing the data frame
print("original dataframe")
print(data_frame)
# adding the
data_frame = ddply(data_frame, .(col2), transform,
percentage=col1/sum(col1) * 100)
# adding the percentage label
data_frame$prcntlabel = paste0(sprintf("%.0f",
data_frame$percentage),
"%")
# printing the modified data frame
print("converted dataframe")
print(data_frame)
# adding graph of plotting data
ggplot(data_frame, aes(x=factor(col2), y=col3, fill=col1)) +
geom_bar(position=position_stack(), stat="identity") +
geom_text(aes(label=prcntlabel), position=position_stack(vjust=0.5), size=2) +
coord_flip()
Output
[1] "original dataframe"
col1 col2 col3
1 5 1 1
2 5 1 2
3 5 1 3
4 5 2 4
5 6 2 5
6 6 2 6
7 6 3 7
8 6 3 8
9 7 3 9
10 7 4 10
11 7 4 11
12 7 4 12
[1] "converted dataframe"
col1 col2 col3 percentage prcntlabel
1 5 1 1 33.33333 33%
2 5 1 2 33.33333 33%
3 5 1 3 33.33333 33%
4 5 2 4 29.41176 29%
5 6 2 5 35.29412 35%
6 6 2 6 35.29412 35%
7 6 3 7 31.57895 32%
8 6 3 8 31.57895 32%
9 7 3 9 36.84211 37%
10 7 4 10 33.33333 33%
11 7 4 11 33.33333 33%
12 7 4 12 33.33333 33%
Similar Reads
How to Show Percentages in Stacked Column Chart in Excel?
In this article, you will learn how to create a stacked column chart in excel. Show percentages instead of actual data values on chart data labels. By default, the data labels are shown in the form of chart data Value (Image 1). But very often user needs to plot charts with actual data and show perc
2 min read
Showing data values on stacked bar chart in ggplot2 in R
In this article, you'll learn how to show data values on a stacked bar chart in ggplot2 in R Programming Language. To show the data into the Stacked bar chart you have to use another parameter called geom_text(). Syntax:  geom_text(size, position = position_stack(vjust = value), colour) Here the si
2 min read
How to create a pie chart with percentage labels using ggplot2 in R ?
In this article, we are going to see how to create a pie chart with percentage labels using ggplot2 in R Programming Language. Packages Used The dplyr package in R programming can be used to perform data manipulations and statistics. The package can be downloaded and installed using the following co
4 min read
How To Fold Legend into Two Rows in ggplot2 in R
In this article, we are going to see how to draw a ggplot2 legend with two Rows in R Programming Language. If we want to draw ggplot2 Legend with two rows, we have to add guides and guide_legend functions to the theme() function. Inside guides() function, we take parameter named color, which has cal
2 min read
Change Y-Axis to Percentage Points in ggplot2 Barplot in R
In this article, we will discuss how to change the Y-axis to percentage using the ggplot2 bar plot in R Programming Language. First, you need to install the ggplot2 package if it is not previously installed in R Studio. To install and load write the below command in R Console : install.packages("ggp
2 min read
Grouped, stacked and percent stacked barplot in ggplot2
The ggplot is a library used for generating graphs in R language. We provide the data and specify the aesthetics as to how the specified data should be mapped. It is a very powerful library and widely used to generate comprehensive graphs and plots. It is used for creating graphics based on the "Gra
5 min read
How To Make Barplots with Error bars in ggplot2 in R?
In this article, we will discuss how to make a barplot with an error bar using ggplot2 in the R programming language. Error Bars helps us to visualize the distribution of the data. Error Bars can be applied to any type of plot, to provide an additional layer of detail on the presented data. Often t
4 min read
How to Add Labels Directly in ggplot2 in R
Labels are textual entities that have information about the data point they are attached to which helps in determining the context of those data points. In this article, we will discuss how to directly add labels to ggplot2 in R programming language. To put labels directly in the ggplot2 plot we add
5 min read
How to fix aspect ratio in ggplot2 Plot in R ?
In this article, we will be looking at the approach to fix the aspect ratio in the ggplot2 plot using functions in the R programming language. The aspect ratio of a data graph is defined as the height-to-width ratio of the graph's size. It can be fixed automatically using the coord_fixed() function
2 min read
How to change legend title in ggplot2 in R?
In this article, we will see how to change the legend title using ggplot2 in R Programming. We will use ScatterPlot. For the Data of Scatter Plot, we will pick some 20 random values for the X and Y axis both using rnorm() function which can generate random normal values, and here we have one more p
3 min read