1

I have stacked bar charts with total frequency, and I simply want to add the percentage for each category in the graph (see link with image below). In other words, I just want to show the percentage for each category below the number that is now reported. For example, instead of seeing "170" in the first bar, I would like to see "170 (85.4%)"; instead of "29", I would like to see "29 (14.6%)"; etc.

This is the code I have now

networkmeasured <- data.frame(supp=rep(c("Article measures network", 
"Article does not measure network"), each=2), Type=rep(c("loosely about 
collab. gov.", "striclty about colla. gov"),2), Articles=c(29, 44, 170, 96))
head(networkmeasured)

#plotting the graph
ggplot(data=networkmeasured, aes(x=Type, y=Articles, fill=supp)) +
geom_bar(stat="identity")+
geom_text(aes(label=Articles), vjust=2, color="white", size=4)+
theme_minimal()

Thanks in advance!

RB.

markus
  • 25,843
  • 5
  • 39
  • 58
  • There are a couple of options; you can get the percentages either inside or outside of ggplot. There's a really helpful explanation [here](https://stackoverflow.com/questions/40249943/adding-percentage-labels-to-a-bar-chart-in-ggplot2). – A. S. K. Jul 10 '19 at 16:39
  • 1
    Possible duplicate of [Adding percentage labels to a bar chart in ggplot2](https://stackoverflow.com/questions/40249943/adding-percentage-labels-to-a-bar-chart-in-ggplot2) – A. S. K. Jul 10 '19 at 16:40
  • Thanks for your help!! – user11766015 Jul 11 '19 at 19:11

1 Answers1

0

As A. S. K. has mentioned, you can do it either inside or outside of ggplot. In your particular example, you could do something like this:

# summarising the data
networkmeasured2 <- networkmeasured %>% 
  group_by(Type, supp) %>% 
  summarise(Articles = sum(Articles)) %>% 
  mutate(perc = round(Articles/sum(Articles)*100, 2))

# plotting the graph  
ggplot(data=networkmeasured2, aes(x=Type, y=Articles, fill=supp)) +
geom_bar(stat="identity") +
geom_text(aes(label = paste0(Articles, " (", perc, "%)")), vjust=2, color="white", size=4) +
theme_minimal()

Example

Arienrhod
  • 2,451
  • 1
  • 11
  • 19