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))
Hello, I believe you'll have to do some data aggregation first. The following will do it. dat2 <- merge(sample.dat, aggregate(value ~ pop, data = sample.dat, FUN = sum), by = "pop") dat2$value.x <- dat2$value.x/dat2$value.y test<-ggplot(dat2, aes(x=response.category,y=value.x, group=pop)) test+geom_bar(stat='identity', position='dodge',aes(fill=pop)) Hope this helps, Rui Barradas Em 10-09-2013 17:23, Simon Kiss escreveu:> 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. >
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.