How to add percentage or count labels above percentage bar plot in R?
In this article, we will discuss how to add percentage or count above percentage bar plot in R programming language.
The ggplot() method of this package is used to initialize a ggplot object. It can be used to declare the input data frame for a graphic and can also be used to specify the set of plot aesthetics. The ggplot() function is used to construct the initial plot object and is almost always followed by components to add to the plot.
ggplot(data, mapping = aes())
- data – The data frame used for data plotting
- mapping – Default list of aesthetic mappings to use for plot.
geom_bar() is used to draw a bar plot.
The geom_bar() method is used which plots a number of cases appearing in each group against each bar value. Using the “stat” attribute as “identity” plots and displays the data as it is. The graph can also be annotated with displayed text on the top of the bars to plot the data as it is.
geom_text(aes(label = ), vjust )
The label can be assigned the value of the column to assign the value to each bar of the plot corresponding to each bar value.
 "Original DataFrame" col1 col2 col3 1 j 1 1 2 d 2 1 3 b 3 1 4 a 4 1 5 g 5 1 6 e 6 1 7 f 7 1 8 i 8 1 9 c 9 1 10 h 10 1
Similarly, percentages can be added to the plot, but in this case, the legend will be continuous, not discrete.
 "Original DataFrame" col1 col2 1 g 1 2 d 2 3 j 3 4 f 4 5 i 5 6 e 6 7 h 7 8 a 8 9 c 9 10 b 10