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). Many, many thanks for any help you may be able to offer! Chandra [[alternative HTML version deleted]]
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
Hi Chandra, You could use ggplot2: library(ggplot2) ggplot(dat, aes(Group_size, Number, fill=Sex)) + geom_bar(stat="summary", fun.y="mean") Best, Ista On Tue, Mar 22, 2011 at 7:30 AM, Chandra Salgado Kent <C.Salgado at cmst.curtin.edu.au> 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). > > > > Many, many thanks for any help you may be able to offer! > > > > Chandra > > > ? ? ? ?[[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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
You can use the tapply function to sum within combinations, then pass the results to barplot (possibly doing a reshape first). Also look at the ggplot2 package, it may do the summing as part of the plot call and probably does not need the reshape step. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Chandra Salgado Kent > Sent: Tuesday, March 22, 2011 1:30 AM > To: r-help at r-project.org > Subject: [R] stacked bar plot > > 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). > > > > Many, many thanks for any help you may be able to offer! > > > > Chandra > > > [[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.
Apparently Analagous Threads
- subset data based on values in multiple columns
- mean fold change issues and p values
- [syzbot] net boot error: WARNING in cpumask_next_wrap
- Stacked bar plot anomaly When column contains a negative and a positive value
- Grouped AND stacked bar charts possible in R?