[R] stacked bar plot

Jim Lemon jim at bitwrit.com.au
Tue Mar 22 10:55:08 CET 2011


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



More information about the R-help mailing list