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.
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)
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)`
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With