How to add boxplots to scatterplot with jitter

前端 未结 4 968
闹比i
闹比i 2020-12-04 16:51

I am using following commands to produce a scatterplot with jitter:

ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T))
library(lat         


        
相关标签:
4条回答
  • 2020-12-04 17:21

    I've written an R function called spreadPoints() within a package basiclotteR. The package can be directly installed into your R library using the following code:

    install.packages("devtools")
    library("devtools")
    install_github("JosephCrispell/basicPlotteR")
    

    For the example provided, I used the following code to generate the example figure below.

    ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T))
    
    boxplot(NUMS ~ GRP, data = ddf, lwd = 2, ylab = 'NUMS')
    
    spreadPointsMultiple(data=ddf, responseColumn="NUMS", categoriesColumn="GRP",
                         col="blue", plotOutliers=TRUE)
    

    It is a work in progress (the lack of formula as input is clunky!) but it provides a non-random method to spread points on the X axis that doubles as a violin like summary of the data. Take a look at the source code, if you're interested.

    0 讨论(0)
  • 2020-12-04 17:30

    For a lattice solution:

    library(lattice)
    ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5], 500, replace = T))
    bwplot(NUMS ~ GRP, ddf, panel = function(...) {
      panel.bwplot(..., pch = "|")
      panel.xyplot(..., jitter.x = TRUE)})
    

    The default median dot symbol was changed to a line with pch = "|". Other properties of the box and whiskers can be adjusted with box.umbrella and box.rectangle through the trellis.par.set() function. The amount of jitter can be adjusted through a variable named factor where factor = 1.5 increases it by 50%.

    0 讨论(0)
  • 2020-12-04 17:31

    To do this in ggplot2, try:

    ggplot(ddf, aes(x=GRP, y=NUMS)) + 
      geom_boxplot(outlier.shape=NA) + #avoid plotting outliers twice
      geom_jitter(position=position_jitter(width=.1, height=0))
    

    ggplot2 version of boxplot + jitter

    Obviously you can adjust the width and height arguments of position_jitter() to your liking (although I'd recommend height=0 since height jittering will make your plot inaccurate).

    0 讨论(0)
  • 2020-12-04 17:43

    Here's one way using base graphics.

    boxplot(NUMS ~ GRP, data = ddf, lwd = 2, ylab = 'NUMS')
    stripchart(NUMS ~ GRP, vertical = TRUE, data = ddf, 
        method = "jitter", add = TRUE, pch = 20, col = 'blue')
    

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