Reputation: 63984
I have a data that looks like this:
ENSG00000211521 MIR665 x 89
....
ENSG00000207793 MIR432 y 50
....
What I want to do is to make a bar plot and add the 'percentage within population' on every bar. For example the value category 'y' with value 100, has the percentage 45.6 (132/289) this is because there are 132 of the one-hundreds and the total population is 289 (for each "x" and "y").
In the end I'd like to have a plot that looks roughly like this:
But I'm stuck with the following code. What's the right way do it?
library(ggplot2)
dat.m <- read.delim("http://dpaste.com/1269939/plain/",sep="")
colnames(dat.m) <- c("ensg","mirna_hgc","variable","value")
qplot(value,data=dat.m, geom="bar", binwidth=1, origin=-0.05, xlim=c(50,100),ylim=c(0,75), facets=variable~.,main="")+
xlab("Value")+
ylab("Frequency")+
theme(legend.position="none")
Update: computing the percentage
The percentage in the graph above can be obtain with this code. But somehow I couldn't find a way to include them into the qplot:
dat.m <- read.delim("http://dpaste.com/1269939/plain/",sep="")
colnames(dat.m) <- c("ensg","mirna_hgc","variable","value")
# the following steps can be applied for "x"
y <- subset(dat.m,dat.m$variable=="y")
y.df <- data.frame(table(y$value))
y.df$percentage <- ((y.df$Freq)/sum(y.df$Freq) * 100)
y.df
Upvotes: 2
Views: 1233
Reputation: 193
You can try this
qplot(value,data=dat.m, geom="bar", binwidth=1, origin=-0.05, xlim=c(50,100),ylim=c(0,75), facets=variable~.,main="")+
xlab("Value")+
ylab("Frequency")+
theme(legend.position="none") +
stat_bin(aes(label = sprintf("%.02f %%", ..count../sum(..count..)*100)),
geom="text")
Some questions related:
Rounding % Labels on bar chart in ggplot2
ggplot: showing % instead of counts in charts of categorical variables
Upvotes: 2