Re: [R] stacked bar plot

From: Chandra Salgado Kent <>
Date: Wed, 23 Mar 2011 14:28:12 +0800

Many thanks!! That's a million times easier!! :-)  

All the best,  


From: on behalf of Ista Zahn Sent: Wed 3/23/2011 12:06 PM
To: Chandra Salgado Kent
Subject: Re: [R] stacked bar plot

FWIW, the ggplot option I suggested works fine with sums instead of means...

.Table<-data.frame(Sex=c("M","F","M","F","F"), Number=c(10,3,1,2,3), Group_size=c(1,1,2,2,2))
ggplot(.Table, aes(Group_size, Number, fill=Sex)) +  geom_bar(stat="summary", fun.y="sum")


On Wed, Mar 23, 2011 at 3:21 AM, Chandra Salgado Kent <> wrote:
> Hello,
> Many thanks for your responses! They were very helpful.
> FYI, ggplot didn't work for me because I needed the sum of the values.
> The fudged option of barplot was very helpful. Since my matrix is extremely large (the example is a subset), and I would need to take a lot of time to insert NAs everywhere as you did, I used the main idea you sent but instead did summed over group sizes. I'm sure this is far from the most efficient way of doing this, but it was the only way I found for my very large matrix.
> Thanks again!!
> Here is my solution:
> #-----------------------------------------------------------------------------------------------------------------------------------------
> .Table<-data.frame(Sex=c("M","F","M","F","F"), Number=c(10,3,1,2,3), Group_size=c(1,1,2,2,2))
> #I separated the females first, and ordered them by group size
> Females<-subset(.Table, Sex=="F")
> .Order<-order(Females$Group_size)
> FemalesF<-rbind(Females$Group_size, Females$Number)[,.Order]
> FemalesF<-t(FemalesF)
> #I then deleted any NAs which I had in my database, then summed Number for each Group_size and converted it to a matrix
> Females1 <- FemalesF[complete.cases(FemalesF[,2]),]
> Females2<-by(FemalesF,FemalesF[,1], FUN = function(x){
> sum(x[,2]) })
> Females3<-matrix(Females2)
> #I then did the same for the males
> Males<-subset(.Table, Sex=="M")
> .Order<-order(Males$Group_size)
> MalesF<-rbind(Males$Group_size, Males$Number)[,.Order]
> MalesF<-t(MalesF)
> Males1 <- MalesF[complete.cases(MalesF[,2]),]
> Males2<-by(MalesF,MalesF[,1], FUN = function(x){
> sum(x[,2]) })
> Males3<-matrix((Males2))
> #I then followed your example in forming a matrix of males and females suitable for barplot and plotted the data
> .Matrix<-matrix(c(Females3,Males3),ncol=2)
> .Matrix<-t(.Matrix)
> barplot(.Matrix,col=c("pink","lightblue"),
> names.arg=c(1:3),xlab="Group size",ylab="Number",main="Group Sex")
> legend(10,60,c("Male","Female"),fill=c("lightblue","pink"))
> #----------------------------------------------------------------------------------------------------------------------------
> Chandra
> ________________________________
> From: Jim Lemon []
> Sent: Tue 3/22/2011 5:55 PM
> To: Chandra Salgado Kent
> Cc:
> Subject: Re: [R] stacked bar plot
> On 03/22/2011 06:30 PM, Chandra Salgado Kent wrote:
>> Hello,
>> I'm wondering if someone may be able to help me, and do apologize if there is a simple and obvious solution for this. I am somewhat new to R, and have been searching for a simple solution for a couple of days.
>> I am interested in finding a tool that allows me to plot a stacked bar plot.
>> My data set is in the following format:
>> data<-data.frame(Sex=c("M","F","M","F","F"), Number=c(10,3,1,2,3), Group_size=c(1,1,2,2,2))
>> I would like to have the factor "Sex" stacked, "Group size" as a Factor on the X axis, and "Number" on the Y axis (summed so that there is only one value for each Sex by Group_size combination).
> Hi Chandra,
> It's a bit hard to work out exactly what you want, but try this:
> barplot(matrix(c(10,3,NA,1,2,3),ncol=2),col=c("lightblue","pink","pink"),
> names.arg=1:2,xlab="Group size",ylab="Number",main="Group Sex")
> legend(1.6,8,c("Male","Female"),fill=c("lightblue","pink"))
> now I have fudged a bit by just making the matrix contain the values in
> the right order, but if the barplot is what you want, it could get you
> started.
> Jim
> [[alternative HTML version deleted]]
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology <> 

	[[alternative HTML version deleted]]

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 23 Mar 2011 - 06:34:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Mar 2011 - 06:40:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive