Re: [R] stacked bar plot

From: Chandra Salgado Kent <C.Salgado_at_cmst.curtin.edu.au>
Date: Wed, 23 Mar 2011 11:21:28 +0800

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 [mailto:jim_at_bitwrit.com.au] Sent: Tue 3/22/2011 5:55 PM
To: Chandra Salgado Kent
Cc: r-help_at_r-project.org
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]]



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. Received on Wed 23 Mar 2011 - 03:25:11 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 - 04:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive