6

I'm trying to reproduce the following image image http://www.davidzeleny.net/wiki/lib/exe/fetch.php/vizualizace:figures:boxplots-jitter-rdbu-colors.png?cache=

The code I'm using is roughly this:

library(RColorBrewer) 
library(reshape2)

a=rnorm(100, mean=1)
b=rnorm(100, mean=0, sd=1)
ab=data.frame(a,b)
melt=melt(ab)
bpColor=brewer.pal(4, 'RdBu')

boxplot(melt$value ~ melt$variable, notch=T, col=c(bpColor[1], bpColor[4]), outline=F, varwidth=T)
stripchart(melt$value ~ melt$variable, add=T, vertical=T, pch=21,
         bg=bpColor[2:3][melt$variable], method='jitter', jitter=0.02)

What I'm getting from this is almost the same except for the color of the stripchart points

my_image http://is.muni.cz/de/256262/Rplot.png

How should I edit my code in order to reproduce the proper coloring? I thought, that

bg=bpColor[2:3][melt$variable]

would do the job, however I'm getting this output, if I would erase the [] brackets I got two colors, but mixed within the groups. Thank you advance for your help.

Henrik
  • 65,555
  • 14
  • 143
  • 159
lukas
  • 349
  • 4
  • 13

3 Answers3

6

Not the most elegant way, but hey, it's working

boxplot(melt$value ~ melt$variable, notch=T, col=c(bpColor[1], bpColor[4]), outline=F, varwidth=T)
stripchart(melt[melt$variable == "a", "value"] ~ melt[melt$variable == "a", "variable"], add=T, vertical=T, pch=21, bg=c(bpColor[2]), method='jitter', jitter=0.02)
stripchart(melt[melt$variable == "b", "value"] ~ melt[melt$variable == "b", "variable"], add=T, vertical=T, pch=21, bg=c(bpColor[3]), method='jitter', jitter=0.02)

enter image description here

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • You're right David it works, however I'd rather have it in the elegant way, since I have no idea why this 'filtering' technique is not working in this particular example. It is working for other cases, so why not now? I'll wait for a while if anyone else will respond and if not, I'll mark it as answered. Thanks! – lukas Apr 08 '14 at 10:50
  • Makes sense, I'd do the same. – David Arenburg Apr 08 '14 at 10:55
  • No one seems to shed some light into the problem, but anyway as you have said it worked :) – lukas Apr 17 '14 at 13:08
6

This was supposed to be a short comment, but it grew a little bit too big. I don't answer your question, but I hope to provide some insight in the behaviour of col and bg in stripchart.

I note two things which seem to explain your issue:

(1) colours in the col and bg arguments are 'allocated' to the points differently. The col colours are used row-wise, whereas bg colours are allocated to the points column-wise.

(2) 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. Together the allocation and recycling rules for bg implies that it is tricky to map bg colours to different levels of x.

# a very simple data set to make it easier to see what's going on
y <- rep(1:3, 3)
x <- rep(c("a", "b", "c"), each = 3)
  • col colours are used row-wise, whereas bg colours are used column wise

    stripchart(y ~ x, pch = 21,
               col = c("red", "orange", "yellow"),
               bg = rep(c("white", "grey", "black")),
               vertical = TRUE, cex = 4, lwd = 5)
    

enter image description here

  • Only the first three col colours are used. Then they are re-cycled

       stripchart(y ~ x, pch = 21,
                  col = c("red", "orange", "yellow",
                          "green", "blue", "purple",
                          "white", "grey", "black"),
                  bg = rep(c("white", "grey", "black")),
                  vertical = TRUE, cex = 4, lwd = 5)`
    

enter image description here

  • Only the first three bg colours are used. Then they are re-cycled. Thus, 'impossible' to map bg colour' to x (grouping varible)

       stripchart(y ~ x, pch = 21,
                  col = c("red", "orange", "yellow"),
                  bg = c("white", "grey", "black",
                         "red", "orange", "yellow",
                         "green", "blue", "purple"),
                  vertical = TRUE, cex = 4, lwd = 5)
    

enter image description here

Just some further tries:

stripchart(y ~ x, pch = 21,
           col = c("red", "orange", "yellow"),
           bg = rep(c("white", "grey", "black"), 3),
           vertical = TRUE, cex = 4, lwd = 5)

stripchart(y ~ x, pch = 21,
           col = c("red", "orange", "yellow"),
           bg = rep(c("white", "grey", "black")),
           vertical = TRUE, cex = 4, lwd = 5)  
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Thank you for the explanation! This would never crossed my mind, that those types of coloring work other way round. At least my insight into R has been extenden:) – lukas Apr 08 '14 at 10:39
  • I was really puzzled by your issue (+1 to your Q by the way) and just had to start to play around with a _really_ simple data set to get the 'logic' of colour allocation, and how it differed between `bg` and `col`. – Henrik Apr 08 '14 at 11:30
0

Not the answer to the base but a ggplot approach:

ggplot(melt, aes(fill=variable, x=variable, y=value)) +
    geom_boxplot(notch = TRUE) + 
    geom_jitter(position = position_jitter(width = .05, height =0), shape=21, size=1.5) +
    scale_fill_hue(l=40) 

I couldn't figure out how to make the hue for the box fill and the point separate. Thought alpha might work but we want to change points to less intensity not more transparent. I'd appreciate an edit here for someone to fill in the missing piece.

enter image description here

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Asked the hue question as a separate question to gain attention from ggplot2 users: http://stackoverflow.com/questions/22921923/ggplot-change-hue-differently-for-2-geoms-covering-same-grouping – Tyler Rinker Apr 07 '14 at 20:02