Dear all, I have a 2 by 2 matrix and I would like to do a barplot with it. (so 2 bars with each having 2 stacks.). I would like to have one colors per stack, so 4 different colors total. The problem is that R is only given me 2 colors (the same two for the bottom stack and the same two for the top stack). Any idea how I can do to have 4 colors? (without using ggplot2 preferably) Here is my code> w <- matrix(table(cutMF12G3),2,2) > w[,1] [,2] [1,] 8 13 [2,] 8 8> barplot(w, main="2012", col=c("red", "green", "blue", "pink"))Thanks in advance for your help. François [[alternative HTML version deleted]]
Hello, According to the code in file src/R/graphics/barplot.R, barplot() will draw column by column, and threfore use as many colors as rows in the matrix. Those colors will be reused for all bars. So I'm not seeing an easy way of doing what you want using base graphics. Not without changing the code for barplot(). Hope this helps, Rui Barradas Em 26-03-2013 18:09, Francois de Ryckel escreveu:> Dear all, > > I have a 2 by 2 matrix and I would like to do a barplot with it. (so 2 bars with each having 2 stacks.). I would like to have one colors per stack, so 4 different colors total. > The problem is that R is only given me 2 colors (the same two for the bottom stack and the same two for the top stack). Any idea how I can do to have 4 colors? (without using ggplot2 preferably) > Here is my code > >> w <- matrix(table(cutMF12G3),2,2) >> w > [,1] [,2] > [1,] 8 13 > [2,] 8 8 > >> barplot(w, main="2012", col=c("red", "green", "blue", "pink")) > > Thanks in advance for your help. > > Fran?ois > > > > [[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. >
You will have to trick barplot into thinking you have four groups:> wmod <- cbind(c(w[,1], 0, 0), c(0, 0, w[,2])) > barplot(wmod, main="2012", col=c("red", "green", "blue", "pink"))---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Francois de Ryckel > Sent: Tuesday, March 26, 2013 1:09 PM > To: r-help at r-project.org > Subject: [R] barplot colors > > Dear all, > > I have a 2 by 2 matrix and I would like to do a barplot with it. (so 2 > bars with each having 2 stacks.). I would like to have one colors per > stack, so 4 different colors total. > The problem is that R is only given me 2 colors (the same two for the > bottom stack and the same two for the top stack). Any idea how I can > do to have 4 colors? (without using ggplot2 preferably) > Here is my code > > > w <- matrix(table(cutMF12G3),2,2) > > w > [,1] [,2] > [1,] 8 13 > [2,] 8 8 > > > barplot(w, main="2012", col=c("red", "green", "blue", "pink")) > > Thanks in advance for your help. > > Frangois > > > > [[alternative HTML version deleted]]
Hi David, Thanks a lot for your advice and helping me tricking barplot. It totally work. Have a great evening! Fran?ois On 26 Mar 2013, at 21:43, David L Carlson wrote:> You will have to trick barplot into thinking you have four groups: > >> wmod <- cbind(c(w[,1], 0, 0), c(0, 0, w[,2])) >> barplot(wmod, main="2012", col=c("red", "green", "blue", "pink")) > > ---------------------------------------------- > David L Carlson > Associate Professor of Anthropology > Texas A&M University > College Station, TX 77843-4352