Daniel Brewer
2009-Jan-20 10:28 UTC
[R] Stacked barplot with two stacked bars besides each other
Hi, I have a particular barplot I would like to generate, but I am having trouble getting it to work. What I would like is in effect two barplots with stacked bars merged into one. For example, I have two samples (yoda1,yoda2) on which I measure whether two variables (var1,var2) are present or absent for a number of measurements on that sample.> var1 <- data.frame(yoda1=c(3,7), yoda2=c(1,9)) > var2 <- data.frame(yoda1=c(8,2), yoda2=c(5,5))For each variable I can plot a barplot> barplot(as.matrix(var1)) > barplot(as.matrix(var2))I would like to join these together, so that for each sample there are two stacked bars next to each other, one for var1 and the other for var2. I was thinking something like:> barplot(list(as.matrix(var1),as.matrix(var2)))would work, but it didn't. Any suggestions you could make would be great. Dan -- ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
Henrique Dallazuanna
2009-Jan-20 10:50 UTC
[R] Stacked barplot with two stacked bars besides each other
Try this: barplot(cbind(as.matrix(var1), as.matrix(var2)), names.arg = LETTERS[1:4]) On Tue, Jan 20, 2009 at 8:28 AM, Daniel Brewer <daniel.brewer@icr.ac.uk>wrote:> Hi, > > I have a particular barplot I would like to generate, but I am having > trouble getting it to work. What I would like is in effect two barplots > with stacked bars merged into one. For example, I have two samples > (yoda1,yoda2) on which I measure whether two variables (var1,var2) are > present or absent for a number of measurements on that sample. > > > var1 <- data.frame(yoda1=c(3,7), yoda2=c(1,9)) > > var2 <- data.frame(yoda1=c(8,2), yoda2=c(5,5)) > > For each variable I can plot a barplot > > > barplot(as.matrix(var1)) > > barplot(as.matrix(var2)) > > I would like to join these together, so that for each sample there are > two stacked bars next to each other, one for var1 and the other for > var2. I was thinking something like: > > > barplot(list(as.matrix(var1),as.matrix(var2))) > > would work, but it didn't. > > Any suggestions you could make would be great. > > Dan > > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer@icr.ac.uk > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a charitable > Company Limited by Guarantee, Registered in England under Company No. 534147 > with its Registered Office at 123 Old Brompton Road, London SW7 3RP. > > This e-mail message is confidential and for use by the...{{dropped:20}}
hadley wickham
2009-Jan-20 13:35 UTC
[R] Stacked barplot with two stacked bars besides each other
On Tue, Jan 20, 2009 at 4:28 AM, Daniel Brewer <daniel.brewer at icr.ac.uk> wrote:> Hi, > > I have a particular barplot I would like to generate, but I am having > trouble getting it to work. What I would like is in effect two barplots > with stacked bars merged into one. For example, I have two samples > (yoda1,yoda2) on which I measure whether two variables (var1,var2) are > present or absent for a number of measurements on that sample. > >> var1 <- data.frame(yoda1=c(3,7), yoda2=c(1,9)) >> var2 <- data.frame(yoda1=c(8,2), yoda2=c(5,5))I'd start by storing your data in a single data frame, with all information explicit: var1$row <- 1:2 var1$var <- "one" var2$row <- 1:2 var2$var <- "two" vars <- rbind(var1, var2) library(reshape) df <- melt(vars, id = c("var", "row")) names(df)[3] <- "yoda" df (In reality you'd give the variables informative names based on your study design) Then you're in a position to better describe and control what you want. With the data in this form, you could then use the ggplot2 package to display it: library(ggplot2) qplot(yoda, value, data = df, fill = factor(row), geom="bar", stat "identity", facets = ~ var) This puts yoda on the x axis, colours the bars by the row and separates the plot into two panels based on var. It's trivial to produce any other arrangement of the three variables. qplot(var, value, data = df, fill = factor(row), geom="bar", stat "identity", facets = ~ yoda) qplot(row, value, data = df, fill = yoda, geom="bar", stat "identity", facets = ~ var) etc Hadley -- http://had.co.nz/