Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point color (col) and fill color (bg) by group in stripchart

Tags:

plot

r

colors

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.

like image 650
lukas Avatar asked Apr 07 '14 18:04

lukas


2 Answers

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)  
like image 28
Henrik Avatar answered Nov 20 '22 17:11

Henrik


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

like image 136
David Arenburg Avatar answered Nov 20 '22 18:11

David Arenburg