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

j. van den hoff veedeehjay at googlemail.com
Mon Apr 20 18:34:47 CEST 2015


On Mon, 20 Apr 2015 18:10:10 +0200, Marc Schwartz <marc_schwartz at me.com>  
wrote:

> On Apr 20, 2015, at 10:01 AM, j. van den hoff  
> <veedeehjay at 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,
>
hi,

thanks for responding.

> 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))

well, that would make it "consistent" but it is not what I want, actually  
...

>
>
> 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.

yes, I see. that indeed would work. however, in my actual use case, I have  
a looong time series with dozens of bars. so it would become quite ugly  
(looping over all the bars subsetting the color vector etc). but yes, one  
could do it this way.

>
> 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.

yes, I was expecting this answer and I understand it, of course. on the  
other hand, it would be rather straightforward to add another argument to  
`barplot' to control the behaviour (while defaulting to the present one).  
I would think this would be a good addition: in my use case, e.g. I do  
have such categorized and grouped data and just want to be able to plot  
them after sorting within each group according to value while maintaining   
unique color assignments independent of whether the data are sorted within  
each group or not. and with `beside=FALSE'  this is completely  
straightforward (just resort the color vector together with the data),  
while it fails with `beside=TRUE'.

>
> You can always make a local modification of barplot() for your own use

yes, that's what I did. but I wonder how many people would benefit from  
the ability of easily plotting the same data with the same color vs. data  
correspondence independent of the `beside' setting. but it's for the devs  
to decide...

> and/or consider that there might be a logical CRAN package for graphics  
> extensions where it could be included as an add-on function.

yes, maybe I check whether this would make sense.

thank you,

joerg

>
> Regards,
>
> Marc Schwartz
>
>


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/



More information about the R-help mailing list