On Apr 20, 2015, at 10:01 AM, j. van den hoff <veedeeh...@googlemail.com> wrote:
> 
> 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


Hi,

You can go the other way:

  layout(1:2)
  barplot(x, beside = FALSE, col = 1:3)
  barplot(x, beside = TRUE, col = rep(1:3, 2))


You could use the following workaround:

  barplot(cbind(1:3, c(NA, NA, NA)), beside = FALSE, col = 1:3, ylim = c(0, 15))
  barplot(cbind(c(NA, NA, NA), 4:6), beside = FALSE, col = 4:6, add = TRUE)

That essentially plots each stack separately, using the respective NA columns 
to space the two stacks in the plot. In the first call, also setting the y axis 
limits to handle both stacks. In the second call, using ‘add = TRUE’ so that 
the second plot does not overwrite the first.

The use of stacked bar plots is typical when trying to visually compare the 
same categories across groupings, thus the same colors in each stack by 
default. This is not always easy if the differences are subtle, similar to the 
issues with pie charts.

I would not advocate changing the current behavior, as a lot of long standing 
code would break, including functions in packages that are built on top of 
barplot() and expect certain default behaviors.

You can always make a local modification of barplot() for your own use and/or 
consider that there might be a logical CRAN package for graphics extensions 
where it could be included as an add-on function.

Regards,

Marc Schwartz

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to