2

This is closely related to another question I just asked.

This is my data:

y <- structure(
  c(0.5619, 0.4381, 0.7587, 0.2413, 0.8764, 0.1236, 
    0.9019, 0.0981, 0.9481, 0.0519, 0.99, 0.01),
  .Dim = c(2L, 6L), .Dimnames = list(c("FALSE", "TRUE"), NULL)
)
y
#         [,1]   [,2]   [,3]   [,4]   [,5] [,6]
# FALSE 0.5619 0.7587 0.8764 0.9019 0.9481 0.99
# TRUE  0.4381 0.2413 0.1236 0.0981 0.0519 0.01

The original plot with same colors within each group (blue and red):

barplot(
  y, horiz = TRUE, col = c("blue", "red"),
  names.arg = c("Overall", paste("Flag", 5:1)), las = 1L,
  cex.names = 0.6,
  main = "Proportion Dropped Given Each Sample Restriction"
)

A plot with 6 vertically-stacked bars titled "Proportion Dropped Given Each Sample Restriction". They are labelled (from top to bottom) "Flag 1", "Flag 2", "Flag 3", "Flag 4", "Flag 5", "Flag 6", and "Overall". Each bar extends from 0.0 to 1.0 on the plot and is split in two; the left part is blue, and the right part is red. The split is in different places for each bar; that for "Flag 1" is furthest to the right, while that for "Overall" is furthest to the left. The split bars are ordered by this split location.

I want to change the red right-hand bars for each group, and instead have different colors within each group, something like:

This plot is very similar to the previous one. The only difference is that, instead of each right-hand portion being colored red, they are all colored distinctly. The left portions are still blue.

I therefore created a new col vector with one color for each bar segment:

barplot(
  y, horiz =TRUE,
  col = c(
    "blue", "gold",
    "blue", "springgreen",
    "blue", "orange",
    "blue", "red",
    "blue", "white"
  ),
  names.arg = c("Overall", paste("Flag", 5:1)), las = 1L,
  cex.names = 0.6,
  main = "Proportion Dropped Given Each Sample Restriction"
)

However, only the first two colors in col (blue and gold) are used and they are recycled 6 times:

The output of the code intended to produce the previous plot. Instead of being distinctly colored, the right-hand portions are all gold.

Is there any way to get the output I'm looking for?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Not an answer to your question, but once noticed a similar recycling of colours in `stripchart` (see [**here**](http://stackoverflow.com/questions/22920381/different-coloring-of-groups-in-r-plot/22923960#22923960) "Only as many colours that are needed to for the points in one row (for `col` colours) or column (for `bg` colours) are picked from the colour vector, then they are recycled.". For `barplot` it seems like "Only as many colours that are needed to for the _bars_ in one _column_ (for `col` colours) are picked from the colour vector, then they are recycled." – Henrik Aug 05 '15 at 19:28
  • Border colors are also recycled. A tiny example: `m <- matrix(1:4, nrow = 2)`; `cols <- c("blue", "red", "green", "yellow")`; `border <- c("red", "blue", "yellow", "green")`; `par(lwd = 6)`; `barplot(m, col = cols, border = border)`. I have seen work-arounds for `base`, but I am not able to find them right now. You may consider `ggplot` and `scale_fill_manual`. – Henrik Aug 05 '15 at 19:31
  • Relevant R-help posts [**here**](https://stat.ethz.ch/pipermail/r-help/2011-July/284640.html) and [**here**](https://stat.ethz.ch/pipermail/r-help/2007-March/126848.html). – Henrik Aug 05 '15 at 19:33
  • @Henrik thanks for the references! – MichaelChirico Aug 05 '15 at 19:41

1 Answers1

2

Perhaps by tricking barplot into thinking there are more categories. This is tremendously ugly, but it seems to get the job done:

ymod <- cbind(c(y[,1], 0, 0,0,0,0,0,0,0,0,0),
              c(0, 0, y[,2],0,0,0,0,0,0,0,0),
              c(0, 0,0,0, y[,3],0,0,0,0,0,0),
              c(0, 0,0,0,0,0, y[,4],0,0,0,0),
              c(0, 0,0,0, 0,0,0,0,y[,5],0,0),
              c(0, 0,0,0, 0,0,0,0,0,0,y[,6]))

barplot(ymod, horiz=T,
        col=c(rbind(rep("blue",6),c("white","red","purple",
                                    "orange","springgreen","gold"))))

enter image description here

Here and here are some references that I used to achieve this result.

Community
  • 1
  • 1
erasmortg
  • 3,246
  • 1
  • 17
  • 34
  • 2
    Well, we can make this much nicer with `magic::adiag`, as inspired by [this](http://stackoverflow.com/questions/17495841/block-diagonal-binding-of-matrices): `ymod<-Reduce(magic::adiag,lapply(split(t(y),1:ncol(y)),as.matrix))` – MichaelChirico Aug 05 '15 at 21:55
  • Yes, beautiful! I struggled with transposing `y` and attempting to fit a pattern that would make it much more readable. This one is it! – erasmortg Aug 05 '15 at 22:44