[R] barplot: segment-wise shading

Jim Lemon jim at bitwrit.com.au
Fri Jan 17 03:21:24 CET 2014


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




More information about the R-help mailing list