[R] barplot: segment-wise shading

Marc Schwartz marc_schwartz at me.com
Thu Jan 16 23:46:24 CET 2014


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

> Dear listers,
> 
> I would like to make stacked barplot, and to be able to define shading
> (density or angle) segment-wise, i.e. NOT like here:
> # Bar shading example
>     barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
>             legend = rownames(VADeaths))
> 
> The example has 5 different angles of shading, I would like to have as
> many possible angle values as there are segments (i.e. 20 in the
> VADeaths example).
> I was not successful using web search.
> Any advice?
> 
> Thank you for your patience.
> With the best regards,
> Martin Weiser


You could do something like this:

# Get the dimensions of VADeaths
> dim(VADeaths)
[1] 5 4

# How many segments?
> prod(dim(VADeaths))
[1] 20


Then use that value in the barplot() arguments as you desire, for example:

  barplot(VADeaths, angle = 15 + 10 * 1:prod(dim(VADeaths)), 
          density = 20, col = "black", legend = rownames(VADeaths))


or wrap the barplot() function in your own, which pre-calculates the values and then passes them to the barplot() call in the function.

See ?dim and ?prod

Be aware that a vector (eg. 1:5) will be 'dim-less', thus if you are going to use this approach for a vector based data object, you would want to use ?length

Regards,

Marc Schwartz




More information about the R-help mailing list