[R] color handling in `barplot' inconsistent betwen `beside=FALSE' and `beside=TRUE'

j. van den hoff veedeehjay at googlemail.com
Mon Apr 20 17:01:46 CEST 2015


hi,

consider the following example:

8<-------------------------------------
x <- matrix(1:6, 3, 2)
layout(1:2)
barplot(x, beside = TRUE, col = 1:6)
barplot(x, beside = FALSE, col = 1:6)
8<-------------------------------------

it seems, it is not possible to make `beside=FAlSE' plots behave the same  
as `beside=TRUE' plots (i.e. use unique colors for all bars or bar  
components), or is it? if I do not miss something, I would say the present  
behaviour (as of 3.1.3) is not (or not always, anyway) desirable. rather,  
`beside=FALSE' should use the same color for all bars or bar components as  
`beside=TRUE'.

any opionions on that?

in case someone needs this, the following patch achieves what I would  
expect from `barplot(beside=FALSE, ...)' -- at least w.r.t. colors, if not  
shading ... -- in the first place:

8<-----------------------------------
@@ -96,12 +96,12 @@
      if (beside)
          w.m <- matrix(w.m, ncol = NC)
      if (plot) {
-        dev.hold()
+        ###dev.hold()
          opar <- if (horiz)
              par(xaxs = "i", xpd = xpd)
          else par(yaxs = "i", xpd = xpd)
          on.exit({
-            dev.flush()
+            ###dev.flush()
              par(opar)
          })
          if (!add) {
@@ -119,10 +119,16 @@
                  w.r, horizontal = horiz, angle = angle, density = density,
                  col = col, border = border)
          else {
+            numelements <- length(height[-1,])
+            numcols <- length(col)
+            if (numelements != numcols)
+               col <- rep_len(col, ceiling(numelements/numcols))
+            col <- col[1:numelements]
+            attr(col, "dim") <- dim(height[-1,])
              for (i in 1L:NC) {
                  xyrect(height[1L:NR, i] + offset[i], w.l[i],
                    height[-1, i] + offset[i], w.r[i], horizontal = horiz,
-                  angle = angle, density = density, col = col,
+                  angle = angle, density = density, col = col[1:NR, i],
                    border = border)
              }
          }
8<-----------------------------------

(please note that this is the diff for the representation of the function  
as it appears in `edit(barplot)', rather than as it appears in the R  
source code ...)

@devs: would it be desirable to change the "official" `barplot' behaviour  
accordingly in the future?


thanks

joerg



--



More information about the R-help mailing list