boxplot strange behavior

77 views
Skip to first unread message

Giovanni Azua

unread,
Nov 16, 2011, 2:09:21 PM11/16/11
to ggp...@googlegroups.com
Hello,

I generate box plots from my data like this:

qplot(x=xxx,y=column,data=data,geom="boxplot") + xlab("xxx") + ylab(ylabel) + theme_bw() + scale_y_log10() + geom_jitter(alpha=I(1/10))

The problem is that I see lot of points below the minimum at the same level as some outliers. I looks very weird as I expected the outliers to be "few" and specially not see any points other than the outliers below the minimum or above the maximum. 

Can anyone explain what's going on?

Attached I send a snapshot, see the second level having some outliers above but separate from the outliers there are also points which seem not to be outliers and that are outside the maximum indicator? is this a bug or am I missing anything?

TIA,
Best regards,
Giovanni

Dennis Murphy

unread,
Nov 16, 2011, 2:30:49 PM11/16/11
to Giovanni Azua, ggp...@googlegroups.com
What you're seeing is the effect of jittering - the outside points (in black) extend from the whiskers, the grey points are the same set of points jittered. If you don't want that effect, then either don't jitter or suppress the outside points inside the geom_boxplot() call; see the outlier options under 'Parameters' in the on-line help page for geom_boxplot:

http://had.co.nz/ggplot2/geom_boxplot.html

Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

PastedGraphic-1.tiff

Winston Chang

unread,
Nov 16, 2011, 2:38:39 PM11/16/11
to Giovanni Azua, ggp...@googlegroups.com
It might be less confusing if you use ggplot instead of qplot, and also use geom_point instead of geom_jitter. The equivalent would probably look something like this:

ggplot(data, aes(x=xxx, y=column)) + geom_boxplot() +
    geom_point(position="jitter")

I think the issue is that you're jittering the points from geom_point, but not the outlier points from geom_boxplot. I don't think it's possible to jitter the outlier points from the boxplot geom.

You should be able to change the outlier size and shape to help deal with the overplotting, with something like this:
  geom_boxplot(outlier.shape = 21, outlier.size = 1)
This will make smaller points and use hollow circles. I think you may need the development version for this, though.

-Winston


--
PastedGraphic-1.tiff

Justin Haynes

unread,
Nov 16, 2011, 3:07:08 PM11/16/11
to Winston Chang, Giovanni Azua, ggp...@googlegroups.com
The native geom_boxplot doesn't support it, but once upon a time I found a post with revised version for jitter and I added alpha also.

GeomBoxplotJitterOutlier <- proto(GeomBoxplot, {
   draw <- function (., data, ..., outlier.colour = "black", outlier.shape = 16, 
    outlier.size = 2, outlier.jitter=0,outlier.alpha=1) {
# copy the body of function 'draw' above and paste here.
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 = outlier.alpha, 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), ...))))

}

  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()

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


Hope that helps,

Justin
PastedGraphic-1.tiff

Giovanni Azua

unread,
Nov 20, 2011, 11:22:11 AM11/20/11
to ggp...@googlegroups.com
Hello Dennis,

Thank you for your answer! it makes sense now.

Best regards,
Giovanni

On Nov 16, 2011, at 8:30 PM, Dennis Murphy wrote:

What you're seeing is the effect of jittering - the outside points (in black) extend from the whiskers, the grey points are the same set of points jittered. If you don't want that effect, then either don't jitter or suppress the outside points inside the geom_boxplot() call; see the outlier options under 'Parameters' in the on-line help page for geom_boxplot:

http://had.co.nz/ggplot2/geom_boxplot.html

Dennis

On Wed, Nov 16, 2011 at 11:09 AM, Giovanni Azua <brav...@gmail.com> wrote:
Hello,

I generate box plots from my data like this:

qplot(x=xxx,y=column,data=data,geom="boxplot") + xlab("xxx") + ylab(ylabel) + theme_bw() + scale_y_log10() + geom_jitter(alpha=I(1/10))

The problem is that I see lot of points below the minimum at the same level as some outliers. I looks very weird as I expected the outliers to be "few" and specially not see any points other than the outliers below the minimum or above the maximum. 

Can anyone explain what's going on?

Attached I send a snapshot, see the second level having some outliers above but separate from the outliers there are also points which seem not to be outliers and that are outside the maximum indicator? is this a bug or am I missing anything?

TIA,
Best regards,
Giovanni

<PastedGraphic-1.tiff>
Reply all
Reply to author
Forward
0 new messages