[R] ggplot2 counts versus percentages
Simon Kiss
sjkiss at gmail.com
Sun Feb 9 17:12:03 CET 2014
Hello: I’m having troubles with plotting a barchart with percentages rather than counts in ggplot2. I’m aware that others have a problem with this, but cannot get this to work as I wish. At the end, I’d like a facetted barchart with percentages rather than with counts. Thank you for any assistance!
I have a data.frame that looks like this below, table the data and then melt it to get it into long format.
#Libraries
l<-c(‘reshape’, ‘ggplot2’)
lapply(l, library, character.only=T)
#Sample
test<-data.frame(society=sample(myvalues, size=100, replace=TRUE), equality=sample(myvalues, size=100, replace=TRUE), discrim=sample(myvalues, size=100, replace=TRUE))
#Long format
test.table<-apply(test, 2, table)
test.table<-melt(test.table)
#And now I do this to create a facetted series of barcharts
ggplot(test.table,aes(x=X1, y=value))+geom_bar(stat=‘identity')+facet_grid(~X2)
#How do I get it to plot percentages, rather than the counts?
#I’ve tried several variations of this to no success
ggplot(test.table,aes(x=X1, y=value))+geom_bar(stat='identity', aes(y=value, (..count..)/sum(..count..)))+facet_grid(~X2)
ggplot(test.table,aes(x=X1))+geom_bar(stat='identity', aes(y=value/(..sum..)/value))+facet_grid(~X2)
Thank you for your assistance!
Yours, Simon Kiss
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
More information about the R-help
mailing list