jitter if multiple outliers in ggplot2 boxplot

前端 未结 5 1412
灰色年华
灰色年华 2020-12-08 06:31

I am trying to find a suitable display to illustrate various properties within and across school classes. For each class there is only 15-30 data points (pupils).

Ri

5条回答
  •  囚心锁ツ
    2020-12-08 06:54

    you can redifine the funcition

    GeomBoxplot$draw<-function (., data, ..., outlier.colour = "black", outlier.shape = 16, 
        outlier.size = 2, outlier.jitter=0) 
    {
        defaults <- with(data, data.frame(x = x, xmin = xmin, xmax = xmax, 
            colour = colour, size = size, linetype = 1, group = 1, 
            alpha = 1, fill = alpha(fill, alpha), stringsAsFactors = FALSE))
        defaults2 <- defaults[c(1, 1), ]
            if (!is.null(data$outliers) && length(data$outliers[[1]] >= 
            1)) {
                pp<-position_jitter(width=outlier.jitter,height=0)
                p<-pp$adjust(data.frame(x=data$x[rep(1, length(data$outliers[[1]]))], y=data$outliers[[1]]),.scale)
            outliers_grob <- GeomPoint$draw(data.frame(x=p$x, y = p$y, colour = I(outlier.colour), 
                shape = outlier.shape, alpha = 1, size = outlier.size, 
                fill = NA), ...)
        }
        else {
            outliers_grob <- NULL
        }
        with(data, ggname(.$my_name(), grobTree(outliers_grob, GeomPath$draw(data.frame(y = c(upper, 
            ymax), defaults2), ...), GeomPath$draw(data.frame(y = c(lower, 
            ymin), defaults2), ...), GeomRect$draw(data.frame(ymax = upper, 
            ymin = lower, defaults), ...), GeomRect$draw(data.frame(ymax = middle, 
            ymin = middle, defaults), ...))))
    }
    
    ggplot(test, aes(x=places,y=value))+geom_boxplot(outlier.jitter=0.05)
    

    This is ad-hoc solution. Of course, in the sense of OOP, you should create a sub-class of GeomBoxplot and override the function. This is easy because ggplot2 is nice.

    === added for example of sub-class definition ===

    GeomBoxplotJitterOutlier <- proto(GeomBoxplot, {
       draw <- function (., data, ..., outlier.colour = "black", outlier.shape = 16, 
        outlier.size = 2, outlier.jitter=0) {
    # copy the body of function 'draw' above and paste here.
    }
    
      objname <- "boxplot_jitter_outlier"
      desc <- "Box and whiskers plot with jittered outlier"
      guide_geom <- function(.) "boxplot_jitter_outlier"
    
    })
    geom_boxplot_jitter_outlier <- GeomBoxplotJitterOutlier$build_accessor()
    

    then you can do with your sub-class:

    ggplot(test, aes(x=places,y=value))+geom_boxplot_jitter_outlier(outlier.jitter=0.05)
    

提交回复
热议问题