[R] barplot: segment-wise shading

Marc Schwartz marc_schwartz at me.com
Fri Jan 17 18:49:19 CET 2014

On Jan 16, 2014, at 9:09 PM, Martin Weiser <weiser2 at natur.cuni.cz> wrote:

> Jim Lemon píše v Pá 17. 01. 2014 v 13:21 +1100:
>> On 01/17/2014 10:59 AM, Marc Schwartz wrote:
>>> ...
>>> Arggh.
>>> No, this is my error for not actually looking at the plot and presuming that it would work.
>>> Turns out that it does work for a non-stacked barplot:
>>>   barplot(VADeaths, angle = 1:20 * 10, density = 10, beside = TRUE)
>>> However, internally within barplot(), actually barplot.default(), the manner in which the matrix is passed to an internal function called xyrect() to draw the segments, is that entire columns are passed, rather than the individual segments (counts), when the bars are stacked.
>>> As a result, due to the vector based approach used, only the first 5 values of 'angle' are actually used, since there are 5 columns, rather than all 20. The same impact will be observed when using the default legend that is created.
>>> Thus, I don't believe that there will be an easy (non kludgy) way to do what you want, at least with the default barplot() function.
>>> You could fairly easily create/build your own function using ?rect, which is what barplot() uses to draw the segments. I am not sure if lattice based graphics can do this or perhaps using Hadley's ggplot based approach would offer a possibility.
>>> Apologies for the confusion.
>>> Regards,
>>> Marc
>> Hi Marc and Martin,
>> When I saw the original message I tried to look at the code for the 
>> barplot function to see if I could call the rectFill function from 
>> plotrix into it. Unfortunately barplot is one of those "internal" 
>> functions that are not at all easy to hack and I have never gotten 
>> around to adding stacked bars to the barp function. I thought that 
>> rectFill would allow you to use more easily discriminated fills than 
>> angles that only differed by 18 degrees.
>> Jim
> Hi,
> after Marc pointed me out where to look for, I hacked barplot.default a
> bit, so now it does what I want (I added "segmentwise" argument).
> Unfortunately, it works well with segmentwise = TRUE, but not with
> segmentwise = FALSE (default)
> With segmentwise = FALSE, density argument works only in 1/n-th of the
> segments, where n is the number of columns (it seems like it refuses to
> auto-multiplicate, but I do not know why).
> Any ideas?
> Martin
> Here is my hack of barplot:

<code snipped>


This would be a good time to learn how to use the ?debug function and related tools to step through your code to see where it is failing. Roger Peng also has some good notes here:


Note that when 'segmentwise = TRUE' and there are no 'angle' or 'density' arguments provided, it also does not work correctly. You may want to set some defaults in that case, or issue an error message.

I suspect that something in the indexing/expansion code that you added is not working as desired, but you will need to step through the code to see where.

One thing that you might want to consider, if the situation that you have is rather specialized, create your own function as you have done, but if 'segementwise = FALSE', then pass the arguments to barplot() so that the default function is used in that situation.



More information about the R-help mailing list