Hi all, I have the following dataset, call it test.data (30 columns, and one row named "0"): ADVP ADVP AP AP CONJ CONJ CP CP DU DU INF INF MWU MWU NP NP PP PP PPRT PPRT REL REL SMN SMN SSB SSB SV1 SV1 TI TI 0 96.85 2.05 89.07 2.54 70.91 2.37 94.92 3.46 82.31 11.33 40.96 2.25 98.06 3.43 90.77 17.63 86.60 10.78 60.27 1.32 93.27 0.97 77.60 12.46 64.98 3.32 88.54 3.70 24.33 0.28 I'm trying to plot these data points so that any pair of identical columns (e.g., ADVP) are plotted closer to each other than any neighbouring pairs. This much is easy to do: data<-as.matrix(read.table("test.data")) colorlist <- c("red") # build basic barplot: barplot(data, beside=FALSE, # don't put bars beside each other col=colorlist, # list of colors xlim = c(1,40), # x-axis limits width = .65, # width of bar plots ylim=c(0, 140), # y-axis limits space = c(1,.5), # space between bars, and between bar-pairs axes = F # don't plot y-axis yet ) This gives a graph with the same color for each bar. The problem is that I want the first instance of a pair of identical columns to have a certain colour, and the other one a different one. So, the bars should really have an alternating pattern like red green red green... and so on, not just one colour. For the above dataset I could set beside=TRUE and transform the dataset to two rows and 15 columns rather than the current 1x30, so something like: ADVP (etc.) 0a 96.85 0b 2.05 but I will eventually need to plot a matrix like ADVP ADVP (etc.) 1 30 40 2 20 20 3 10 5 and there I will want beside=FALSE because I want the 1, 2, 3 rows to appear *within* a bar, but I still want pairs of bars for ADVP. This is why I don't want to set beside=FALSE. I'll be very grateful for any suggestions. Many thanks, -- Dr. Shravan Vasishth Phone: +49 (681) 302 4504 Computational Linguistics, Universit?t des Saarlandes, Postfach 15 11 50 D-66041 Saarbr?cken, Germany http://www.coli.uni-sb.de/~vasishth -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Hi all, > > I have the following dataset, call it test.data (30 columns, and onerow> named "0"): > > ADVP ADVP AP AP CONJ CONJ CP CP DU DU INF INFMWU> MWU NP NP PP PP PPRT PPRT REL REL SMN SMN SSB SSB SV1 > SV1 TI TI > 0 96.85 2.05 89.07 2.54 70.91 2.37 94.92 3.46 82.31 11.33 40.96 2.2598.06> 3.43 90.77 17.63 86.60 10.78 60.27 1.32 93.27 0.97 77.60 12.46 64.983.32> 88.54 3.70 24.33 0.28 > > I'm trying to plot these data points so that any pair of identicalcolumns> (e.g., ADVP) are plotted closer to each other than any neighbouringpairs.> > This much is easy to do: > > data<-as.matrix(read.table("test.data")) > > colorlist <- c("red") > > # build basic barplot: > > barplot(data, > beside=FALSE, # don't put bars beside each other > col=colorlist, # list of colors > xlim = c(1,40), # x-axis limits > width = .65, # width of bar plots > ylim=c(0, 140), # y-axis limits > space = c(1,.5), # space between bars, and between bar-pairs > axes = F # don't plot y-axis yet > ) > > This gives a graph with the same color for each bar. > > The problem is that I want the first instance of a pair of identical > columns to have a certain colour, and the other one a different one.So,> the bars should really have an alternating pattern like red green red > green... and so on, not just one colour. > > For the above dataset I could set beside=TRUE and transform thedataset to> two rows and 15 columns rather than the current 1x30, so somethinglike:> > ADVP (etc.) > 0a 96.85 > 0b 2.05 > > but I will eventually need to plot a matrix like > > ADVP ADVP (etc.) > 1 30 40 > 2 20 20 > 3 10 5 > > and there I will want beside=FALSE because I want the 1, 2, 3 rows to > appear *within* a bar, but I still want pairs of bars for ADVP. > > This is why I don't want to set beside=FALSE. > > I'll be very grateful for any suggestions.The "list" of colors that you have specified for colorlist contains only one entry. If you specify: colorlist <- c("red", "blue", ...) You bars or their stacked segments will cycle through the colors, depending upon whether beside is TRUE or FALSE, respectively. Just be sure to specify one color for each group in your data. HTH. Marc -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Original Message snipped > > Again, keep in mind that all of the bar segments for all of the barsare being> drawn inside the *single* call to xyrect(), thus you need to pass thefull vector> of three colors to xyrect() at once if you want each segment to be adifferent> color.A quick clarification, in that the single xyrect() call is drawing the segments for each bar individually and it is the "i" loop that cycles through each bar. Apologies. I mis-typed the process, though my head knew what I meant... ;-) Marc -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I forgot to say in the summary below how mybarplot2 is called: mybarplot2(data2, # or data3 beside=FALSE, col= colormatrix, width = c(1,.55), space = c(2.75,.25), axes = F) -- Dr. Shravan Vasishth Phone: +49 (681) 302 4504 Computational Linguistics, Universit?t des Saarlandes, Postfach 15 11 50 D-66041 Saarbr?cken, Germany http://www.coli.uni-sb.de/~vasishth ---------- Forwarded message ---------- Date: Tue, 30 Jul 2002 11:33:08 +0200 (MEST) From: Shravan Vasishth <vasishth at gnome.at.coli.uni-sb.de> To: Marc Schwartz <mschwartz at medanalytics.com> Cc: r-help at stat.math.ethz.ch Subject: RE: [R] Barplot coloring question Hi all, I summarize below the eventual solution I used following the discussion in this thread, in case others are interested. This also answers Marc's question below (why the inner loop?). The problem: I need to build two kinds of plots automatically; I need to keep things as automated as possible, since a lot of plots will be generated in the future and we just want to see the output quickly without having to fiddle with the plots individually. The first kind's initial data looks like this:> data2A A B B C C 0 5 10 15 20 70 90 The second kind's data looks like this:> data3A A B B C C 1 5 10 15 20 70 90 2 20 15 24 56 34 70 3 40 54 23 67 12 78 For uniformity, we want to keep the format of the data identical in both cases. In both cases, I want the distance between the bars A-A, B-B, C-C, to be closer to each other than the A-B, B-C distance, and I want to alternate colors, so that the first A has color x and the second A has a different color y. The x, y colors must alternate after that: the first B has x, then the second B has y, etc. However, in data3, I also want the three rows for a given column to appear *within* a single bar, in three colors, say, c1, c2, c3, and the column next to this column should have colors c4, c5, c6. So, now we want the triplets <c1, c2, c3>, <c4,c5,c6> alternating for each successive bar. The reason for this is that we want to display two related but different percentages in the same graph, and we want to do this because then we can quickly eyeball the graph and get maximum information from a single graph, rather than having to pore over dozens of them and to compare bars pairwise across different graphs (a sure way to lose your mind). Solution: (for the reasons why this should be so, see Marc's previous responses) For data2, barplot is rewritten as mybarplot1 (thanks to Marc) as follows: ... if (beside) xyrect(0, w.l, c(height), w.r, horizontal = horiz, col = col) else { for (i in 1:NC) { xyrect(height[1:NR, i], w.l[i], height[-1, i], w.r[i], horizontal = horiz, col = col[i]) } ... For data3, barplot is rewritten as mybarplot2 (again, thanks to Marc) as follows: ... if (beside) xyrect(0, w.l, c(height), w.r, horizontal = horiz, col = col) else { for (i in 1:NC) { for (j in 1:NR) { mycol = col[,i] xyrect(height[1:NR, i], w.l[i], height[-1, i], w.r[i], horizontal = horiz, col = mycol) } } ... Now, one nice thing is that we can just use the above modification, i.e., mybarplot2, for both data2 and data3, we don't need mybarplot1 any more. Not a real big deal, but now we can use the same R script for all our data, irrespective of whether it comes in in data2 or data3 format. Just might make things easier for other people who will one day have to read and/or modify this and related code at our end here. On Mon, 29 Jul 2002, Marc Schwartz wrote:> The question this whole process now raises is why the inner loop?Does the above explanation answer that question?> I am still a little confused as to how you want the colors structured. > That is do you want each bar to contain the three colors, one for each > segment, or do you want each of the three pairs of bars to be colored > differently?I want them to alternate, for data3, as follows: c3 c6 c3 c6 c2 c5 c2 c5 c1 c4 c1 c4 ... A A B B Marc, I want to thank you again for the enormous amount of time you put into this, and for your extraordinary patience as you educated me on these revisions to the code. It would have taken much, much longer to figure all this out without your help. -- Dr. Shravan Vasishth Phone: +49 (681) 302 4504 Computational Linguistics, Universit?t des Saarlandes, Postfach 15 11 50 D-66041 Saarbr?cken, Germany http://www.coli.uni-sb.de/~vasishth -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._