Bar charts with percentage labels but counts on the y axis

June 11, 2014

(This article was first published on socialdatablog » R, and kindly contributed to R-bloggers)

Bar charts and histograms are easily to understand. I often write for non-specialist audiences so I tend to use them a lot. People like percentages too, so a bar chart with counts on the y axis but percentage labels is a useful thing to be able to produce.

But how to do them in our graphics package of choice, ggplot? ggplot makes histograms by doing a stat transformation, stat_bin. Not sure whether this is a good design decision, rather than requiring the user to bin the data first, because it is harder to customise stat_bin to get what you want, in this case percentage labels.



geom_bar(aes(y = (..count..)),fill="orange")+
geom_text(aes(y = (..count..),label =   ifelse((..count..)==0,"",scales::percent((..count..)/sum(..count..)))), stat="bin",colour="darkgreen") 




To leave a comment for the author, please follow the link and comment on their blog: socialdatablog » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)