Reputation: 1067
I have the following R code:
library(ggplot2)
library(reshape2)
protsComp = c(232,62,53,56,61)
protsPart = c(238,64,54,58,62)
percComp = c(93.55,93.94,94.64,91.80,93.85)
percPart = c(95.97,96.97,96.43,95.08,95.38)
xaxis = c("Total", "Group 1", "Group 2", "Group 3", "Group 4")
cegma1 = data.frame(xaxis, Complete = c(232,62,53,56,61), Partial = c(238,64,54,58,62))
cegma.long = melt(cegma1)
ggplot(cegma.long, aes(xaxis, value, fill=variable)) + geom_bar(position="dodge", stat="identity", color="black") +
geom_text(aes(x=xaxis, y=value, label=value, vjust=3.5), position = position_dodge(width=0.9))
With percX containing a percentage value of the corresponding entries in protsX. I manage to get the absolute value as text added to the bars, but how can I add the percentages for each bar instead?
Upvotes: 0
Views: 6131
Reputation: 1643
I like to use the percent
function from the scales
package:
library(ggplot2)
library(reshape2)
library(scales)
protsComp = c(232,62,53,56,61)
protsPart = c(238,64,54,58,62)
percComp = c(93.55,93.94,94.64,91.80,93.85)
percPart = c(95.97,96.97,96.43,95.08,95.38)
xaxis = c("Total", "Group 1", "Group 2", "Group 3", "Group 4")
cegma1 = data.frame(xaxis, Complete = c(232,62,53,56,61), Partial = c(238,64,54,58,62))
cegma.long = melt(cegma1)
ggplot(cegma.long, aes(xaxis, value, fill=variable)) +
geom_bar(position="dodge", stat="identity", color="black") +
geom_text(aes(x=xaxis, y=value, label = percent(value/100), vjust=3.5), position = position_dodge(width=0.9))
Upvotes: 2