Skip to content
Prev 350206 / 398506 Next

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

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