Re: [R] Stacked Bar
Deb, Others have answered your question, I just want to give you something else to think about. Stacked bar plots may not be the best tool to use for this situation, they are ok for comparing the bottom and top groups, but all the other groups don't line up in a stacked bar plot making those comparisons harder. The fact that you want 15 different colors means that this is even more likely the case. You also need to be careful that your colors are different enough from each other that they can be easily distinguished (even when a bar or 2 between them is so small as to not appear). You may want to consider dotplots as an alternative, you can use symbols (possibly numbers or initial letters) in place of or along with the colors. You can also format the dotplot so that the most interesting comparisons are the easiest to make. See the dotplot function in the lattice package and the dotchart2 function in the Hmisc package. If you send us more detail on what you are trying to show (maybe even some simulated data in the same form as your real data), then we can give additional suggestions on how to display the data. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Deb Midya Sent: Tuesday, August 21, 2007 5:29 AM To: r-help@stat.math.ethz.ch Subject: [R] Stacked Bar Hi R Users! Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below. 1. How can I provide 15 different colors for each method with 15 Rows? 2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? Once again thank you very much for your time. Regards, Debabrata (Deb) Statistician NSW Department of CommerceSydney, Australia. The Code: library(lattice) library(graphics) x - matrix(1:75, ncol= 5) dimnames(x)[[2]] - paste(Method, 1:5, sep=) dimnames(x)[[1]] - paste(Row, 1:15, sep=) # library: graphics barplot(x, beside=FALSE, col= 1:nrow(x), legend= rownames(x) ) # library: lattice barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, auto.key = TRUE) - Park yourself in front of a world of choices in alternative vehicles. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
[R] Stacked Bar
Hi R Users! Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below. 1. How can I provide 15 different colors for each method with 15 Rows? 2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? Once again thank you very much for your time. Regards, Debabrata (Deb) Statistician NSW Department of CommerceSydney, Australia. The Code: library(lattice) library(graphics) x - matrix(1:75, ncol= 5) dimnames(x)[[2]] - paste(Method, 1:5, sep=) dimnames(x)[[1]] - paste(Row, 1:15, sep=) # library: graphics barplot(x, beside=FALSE, col= 1:nrow(x), legend= rownames(x) ) # library: lattice barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, auto.key = TRUE) - Park yourself in front of a world of choices in alternative vehicles. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked Bar
Deb Midya wrote: Hi R Users! Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below. 1. How can I provide 15 different colors for each method with 15 Rows? 2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? Hi Deb, As you have probably noticed, the integer coded colors repeat too quickly for the number of colors you want. You can use the rainbow() function to generate colors like this: barplot(x,beside=FALSE,col=rainbow(nrow(x))) or there are lots of other color generating functions in the grDevices or plotrix packages. Here's how to get your legend in an empty space for your plot. There is also an emptyspace() function in the plotrix package that tries to find the biggest empty space in a plot, although it probably wouldn't work in this case. legend(0,1000,rownames(x),fill=rainbow(nrow(x))) Jim __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked Bar
Jim, Thanks for such a quick response. It works well. Is it possible to fill the bars with patterns and colours? Regards, Deb Jim Lemon [EMAIL PROTECTED] wrote: Deb Midya wrote: Hi R Users! Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below. 1. How can I provide 15 different colors for each method with 15 Rows? 2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? Hi Deb, As you have probably noticed, the integer coded colors repeat too quickly for the number of colors you want. You can use the rainbow() function to generate colors like this: barplot(x,beside=FALSE,col=rainbow(nrow(x))) or there are lots of other color generating functions in the grDevices or plotrix packages. Here's how to get your legend in an empty space for your plot. There is also an emptyspace() function in the plotrix package that tries to find the biggest empty space in a plot, although it probably wouldn't work in this case. legend(0,1000,rownames(x),fill=rainbow(nrow(x))) Jim - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked Bar
I think you want to use the 'density' argument. For example: barplot(1:5,col=1) legend(topleft,fill=1,legend=text,cex=1.2) par(new=TRUE) barplot(1:5,density=5,col=2) legend(topleft,fill=2,density=20,legend=text,bty=n,cex=1.2) (if you wanted to overlay solid colors with hatching) Here's the lattice alternative of the bar graph, though the help page says 'density' is currently unimplemented (Package lattice version 0.16-2). To get the legend into columns, I followed the suggestion described here: http://tolstoy.newcastle.edu.au/R/help/05/04/2529.html Essentially I use mapply() and the line following to create a list with alternating 'text' and 'rect' arguments (3 times to get 3 columns). === x - matrix(1:75, ncol= 5) dimnames(x)[[2]] - paste(Method, 1:5, sep=) dimnames(x)[[1]] - paste(Row, 1:15, sep=) u - mapply(function(x,y) list(text=list(lab=x),rect=list(col=y)), x = as.data.frame(matrix(levels(as.data.frame.table(x)$Var1), ncol=3)), y = as.data.frame(matrix(rainbow(nrow(x)), ncol=3)), SIMPLIFY=FALSE) key - c(rep=FALSE,space=bottom,unlist(names-(u,NULL),rec=FALSE)) barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, col=rainbow(nrow(x)),density=5, key = key ) === (I often use tim.colors() in the 'fields' package, if you wanted other ideas for color schemes). --- Deb Midya [EMAIL PROTECTED] wrote: Jim, Thanks for such a quick response. It works well. Is it possible to fill the bars with patterns and colours? Regards, Deb Jim Lemon [EMAIL PROTECTED] wrote: Deb Midya wrote: Hi R Users! Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below. 1. How can I provide 15 different colors for each method with 15 Rows? 2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? Hi Deb, As you have probably noticed, the integer coded colors repeat too quickly for the number of colors you want. You can use the rainbow() function to generate colors like this: barplot(x,beside=FALSE,col=rainbow(nrow(x))) or there are lots of other color generating functions in the grDevices or plotrix packages. Here's how to get your legend in an empty space for your plot. There is also an emptyspace() function in the plotrix package that tries to find the biggest empty space in a plot, although it probably wouldn't work in this case. legend(0,1000,rownames(x),fill=rainbow(nrow(x))) Jim - [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Comedy with an Edge to see what's on, when. __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked Bar
On 8/21/07, Stephen Tucker [EMAIL PROTECTED] wrote: I think you want to use the 'density' argument. For example: barplot(1:5,col=1) legend(topleft,fill=1,legend=text,cex=1.2) par(new=TRUE) barplot(1:5,density=5,col=2) legend(topleft,fill=2,density=20,legend=text,bty=n,cex=1.2) (if you wanted to overlay solid colors with hatching) Here's the lattice alternative of the bar graph, though the help page says 'density' is currently unimplemented (Package lattice version 0.16-2). Yes, and that's unlikely to change unless grid begins to support it. To get the legend into columns, I followed the suggestion described here: http://tolstoy.newcastle.edu.au/R/help/05/04/2529.html Essentially I use mapply() and the line following to create a list with alternating 'text' and 'rect' arguments (3 times to get 3 columns). === x - matrix(1:75, ncol= 5) dimnames(x)[[2]] - paste(Method, 1:5, sep=) dimnames(x)[[1]] - paste(Row, 1:15, sep=) u - mapply(function(x,y) list(text=list(lab=x),rect=list(col=y)), x = as.data.frame(matrix(levels(as.data.frame.table(x)$Var1), ncol=3)), y = as.data.frame(matrix(rainbow(nrow(x)), ncol=3)), SIMPLIFY=FALSE) key - c(rep=FALSE,space=bottom,unlist(names-(u,NULL),rec=FALSE)) barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, col=rainbow(nrow(x)),density=5, key = key ) === A more transparent solution (IMO) is something like barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, par.settings = list(superpose.polygon = list(col=rainbow(nrow(x, auto.key = list(space = right, columns = 2) ) -Deepayan __ R-help@stat.math.ethz.ch 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.