2

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: enter image description here

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
neversaint
  • 60,904
  • 137
  • 310
  • 477
  • possible duplicate of [Adding labels to ggplot bar chart](http://stackoverflow.com/questions/11653268/adding-labels-to-ggplot-bar-chart) – Roman Luštrik Jun 25 '13 at 06:15
  • or this: http://stackoverflow.com/questions/12386005/r-stacked-percentage-bar-plot-with-percentage-of-binary-factor-and-labels-with – Jonas Tundo Jun 25 '13 at 07:05

1 Answers1

2

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

Community
  • 1
  • 1
eyanquenb
  • 193
  • 7
  • Hi thanks. But I couldn't get the percentage right. It gave me 10.9% instead of ~45%. How can I modify your code? – neversaint Jun 25 '13 at 07:14
  • you can change `sum(..count..)*100` for `28900` which is your total population to figure out if the issue is due to the total count – eyanquenb Jun 25 '13 at 07:24