[R] ggplot2 percentages of subpopulations

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 10 20:33:09 CEST 2013


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.
>



More information about the R-help mailing list