Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display percentage label on top of each bar [duplicate]

I am novice to R.I am using ggplot to display bar plot.I want to display percentage of top of every bar.I don't want to convert y-axis to percentage label.Rather i want to display frequency in y-axis and percentage of every group on top of bar.

My data Snippet is

And my desired output snippet is

Any helps are welcomed

like image 501
stackoverflowuser Avatar asked Jun 19 '13 06:06

stackoverflowuser


People also ask

How do I add percentages on top of bars in Excel?

Select the decimal number cells, and then click Home > % to change the decimal numbers to percentage format. 7. Then go to the stacked column, and select the label you want to show as percentage, then type = in the formula bar and select percentage cell, and press Enter key. 8.

How do I show percentage in stacked bar chart in Excel?

Goto “Chart Design” >> “Add Chart Element” >> “Data Labels” >> “Center”. You can see all your chart data are in Columns stacked bar. Step 5: Steps to add percentages/custom values in Chart. Create a percentage table for your chart data.


1 Answers

This kind of plot can be created easily with ggplot2.

dat <- data.frame(Frequency = c(10, 5, 97, 47, 50),
                  Fruits = gl(5, 1, labels = c("Mango", "Apple", "Orange", 
                                               "Guava", "Papaya")))


library(ggplot2)
ggplot(dat, aes(x = Fruits, y = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("%.2f%%", Frequency/sum(Frequency) * 100)), 
            vjust = -.5)

enter image description here

like image 99
Sven Hohenstein Avatar answered Sep 19 '22 23:09

Sven Hohenstein