Re: [R] Help with color coded bar graph
Luis Naver wrote: I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. Hi Luis, I understood your request as wanting a single horizontal bar with 10 segments, each colored according to the value of A. If this is correct, you might want: library(plotrix) plot(1,xlim=c(-1,1),ylim=c(-1,1),xlab=,ylab=,type=n,axes=FALSE) gradient.rect(-1,-0.1,1,0.1,col=grey(c(0.1,0.5,0.9))[A+2]) 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.
[R] Help with color coded bar graph
I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. - Luis Naver __ 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] Help with color coded bar graph
On Fri, 2007-09-07 at 12:45 -0700, Luis Naver wrote: I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. - Luis Naver How about this: barplot(rep(1, length(A)), col = black, space = 0, border = 0) barplot(A, col = grey(0.9), space = 0, border = 0, add = TRUE) The first call sets the plot region to black, ensuring that the x and y axes are consistent with the second call. Alternatively, you can use barplot2() in the gplots CRAN package to do this in a single call, as it has an argument to color the plot region. HTH, Marc Schwartz __ 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] Help with color coded bar graph
On Fri, 7 Sep 2007, Luis Naver wrote: I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() If I understand you correctly, you want a sequence of bars with equal height and colors coded by A (treated like a factor). So Maybe something like cA - grey.colors(3)[factor(A)] barplot(rep(1, length(A)), col = cA, border = cA) or barplot(rep(1, length(A)), col = cA, border = cA, space = 0, xaxs = i, axes = FALSE) ? hth, Z However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. - Luis Naver __ 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.
Re: [R] Help with color coded bar graph
On Fri, 2007-09-07 at 15:07 -0500, Marc Schwartz wrote: On Fri, 2007-09-07 at 12:45 -0700, Luis Naver wrote: I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. - Luis Naver How about this: barplot(rep(1, length(A)), col = black, space = 0, border = 0) barplot(A, col = grey(0.9), space = 0, border = 0, add = TRUE) The first call sets the plot region to black, ensuring that the x and y axes are consistent with the second call. Alternatively, you can use barplot2() in the gplots CRAN package to do this in a single call, as it has an argument to color the plot region. Actually, here is an easier way: barplot(rep(1, length(A)), col = ifelse(A == 0, black, grey(0.9)), space = 0, border = 0) Just set 'col' based upon the value in 'A'. HTH, Marc __ 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] Help with color coded bar graph
Thanks to all who replied (and very quickly). Unfortunatly I was not clear enough as to my intentions. My goal is to replicate a graph I saw in the work by Perry, Miller and Enright in A comparison of methods for the statistical analysis of spatial point patterns in plant ecology (http://www.springerlink.com/content/ 013275pp7376v0hx). For those without the article here is a copy of the graph in question (replicated without permission) http:// img511.imageshack.us/img511/8720/barexamplejl8.png. As you can see in the example, there are several hoizontal bars, colored by the values in an array (one for each bar). I've been thinking of following your examples but setting it to stack, such that all the elements would be placed one on top another. While this may work it seems particularly ungraceful. Again, thanks for the help. -Luis Naver On Sep 7, 2007, at 1:21 PM, Marc Schwartz wrote: On Fri, 2007-09-07 at 15:07 -0500, Marc Schwartz wrote: On Fri, 2007-09-07 at 12:45 -0700, Luis Naver wrote: I have a list of observations that are -1, 1 or 0. I would like to represent them in a horizontal bar color coded based on value like a stacked bar graph. I can achieve this in the form of a png with the following code: A = floor(runif(10)*3) - 1 png(width=100, height=10) par(mar=c(0,0,0,0)) image(matrix(A), col=grey(c(0.1, 0.5, 0.9))) dev.off() However I would like to do this with one of the standard plotting tools (i.e. barplot) to take advantage of labels and multiple series. Any help would be appreciated. - Luis Naver How about this: barplot(rep(1, length(A)), col = black, space = 0, border = 0) barplot(A, col = grey(0.9), space = 0, border = 0, add = TRUE) The first call sets the plot region to black, ensuring that the x and y axes are consistent with the second call. Alternatively, you can use barplot2() in the gplots CRAN package to do this in a single call, as it has an argument to color the plot region. Actually, here is an easier way: barplot(rep(1, length(A)), col = ifelse(A == 0, black, grey(0.9)), space = 0, border = 0) Just set 'col' based upon the value in 'A'. HTH, Marc __ 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.