Different color groups in the R-chart

I am 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 something like 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 get from this is pretty much the same except for the color of the cut points

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

How can I change my code to reproduce the correct color? I thought that

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

will do the job, however I get this conclusion if I remove the [] brackets, I have two colors, but they are mixed inside groups. Thank you for your help.

+4
source share
3 answers

Not the most elegant way, but hey, it works

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

+6

, , . , col bg stripchart.

, , , (1), col bg "" . col -, bg . (2) , ( col) ( bg), , . bg , bg x.

# a very simple data set to make it easier to see what going on
y <- rep(1:3, 3)
x <- rep(c("a", "b", "c"), each = 3)
  • col -, bg 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

  • col.

       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

  • bg. . , "" bg " x ( 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

:

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)  
+5

, ggplot:

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) 

, , . , , . , - .

enter image description here

0

Source: https://habr.com/ru/post/1535425/


All Articles