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

后端 未结 3 2084
余生分开走
余生分开走 2020-12-11 19:44

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 c

相关标签:
3条回答
  • 2020-12-11 20:17

    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)  
    
    0 讨论(0)
  • 2020-12-11 20:34

    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

    0 讨论(0)
  • 2020-12-11 20:34

    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

    0 讨论(0)
提交回复
热议问题