[R] ggplot2 percentages of subpopulations

arun smartpink111 at yahoo.com
Tue Sep 10 21:10:10 CEST 2013


Sorry, a mistake:
library(plyr) #instead of library(reshape2)




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Simon Kiss <sjkiss at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, September 10, 2013 3:08 PM
Subject: Re: [R] ggplot2 percentages of subpopulations

Hi,
Try:
library(reshape2)

 sample.dat1<-ddply(sample.dat,.(pop),mutate, valueper=(value/sum(value))*100)
 test<-ggplot(sample.dat1, aes(x=response.category,y=valueper, group=pop))
 test+geom_bar(stat='identity', position='dodge',aes(fill=pop))
A.K.



----- Original Message -----
From: Simon Kiss <sjkiss at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, September 10, 2013 12:23 PM
Subject: [R] ggplot2 percentages of subpopulations

Hi there: 
I have a sample data set that looks like below.  The variable 'value' represents the counts of cases in each response category.  And I would like to get the barchart to graph the number of responses as a percentage of each total *subpopulation* (Males compared to Females), rather than as a percentage of *all* the responses.
Can someone provide a suggestion?
Thank you

Yours, Simon Kiss
#Sample Code
sample.dat<-data.frame(response.category=rep(c('A', 'B','C'), 2), value=c(50,25,25, 25,25,25), pop=c(rep('Males', 3), rep('Females', 3)))
#Draw GGPLot
test<-ggplot(sample.dat, aes(x=response.category,y=value, group=pop))
test+geom_bar(stat='identity', position='dodge',aes(fill=pop))

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list