0

I made the following graph using ggplot2. What I seek is to add labels with percentages to each of the bars.

cat.ocup.grafico<-ggplot(subset(casen8va, !is.na(o15)), 
aes(factor(o15), weight=expr))+
geom_bar(aes(y =(..count..)/sum(..count..)), fill="blue")+
ylab("Porcentaje")+xlab("Categoría Ocupacional")+coord_flip()

plot1

To do that I tried with the following code. But for some reason, the bars are erased.

Can someone help me?

cat.ocup.grafico+geom_text(aes(label =ifelse((..count..)==0,"",
scales::percent((..count..)/sum(..count..)))), 
stat="count",colour="darkgreen")

plot2

rcs
  • 67,191
  • 22
  • 172
  • 153
Héctor Garrido
  • 185
  • 1
  • 14
  • 1
    can you `dput()` your data.frame so we can reproduce your problem? – agustin Apr 29 '16 at 06:34
  • 1
    People are more likely to help you if you [write a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451#5965451). Columns names like `..count..` are likely to cause a problem. Try renaming this column to `count` before making the plot. – Paul Rougieux Apr 29 '16 at 06:40
  • duplicate ? http://stackoverflow.com/questions/3695497/ggplot-showing-instead-of-counts-in-charts-of-categorical-variables/14511974#14511974 – bVa Apr 29 '16 at 06:58
  • 3
    See the Porcentaje axis. First case has the scale between 0 and ~0.7, while on the second, it's between 0 and 50000. The bars are there, just not visible. :) – Roman Luštrik Apr 29 '16 at 07:46
  • @PaulRougieux `..count..` is a special variable in `ggplot2`. Please, see the related questions http://stackoverflow.com/questions/14570293/special-variables-in-ggplot-count-density-etc and http://stackoverflow.com/questions/15556069/documentation-for-special-variables-in-ggplot-count-density-etc – Uwe Apr 29 '16 at 16:43

0 Answers0