Question

I've looked around the net and found lots of stuff about jittering and changing the shape of outliers but can't seem to find anything about this specific problem.

I want a black and white boxplot with jittered data points - I can do that.

I would also like to change the shape of outliers. Although there are multiple cases with a score of 4 only one of them changes to a hollow circle.

I would assume that if one data point at a particular level is considered an outlier the rest would be considered outliers too.

Is this a coding error or did I miss something along the way in a stats class? If it's a coding thing how do I get all of them to be hollow?

Apparently my "reputation" needs to be 10 to get attach an image! I hope it makes sense without it though

Here's my code:

plot <- ggplot(phase2.3, aes(Group, Score))

plot + geom_point (position = position_jitter(w = 0.1, h = 0.2)) + 
 geom_boxplot (outlier.shape = 1) + xlab("Group") + theme_bw(20)
Was it helpful?

Solution

You probably have to calculate, which points are outside the range by yourself. Here is an extension of the standard example from geom_boxplot that shows how to find the outliers using plyr.

# load packages
require(plyr)
require(ggplot2)
# find outliers
df <- ddply(mtcars, "cyl", function(x){
  iqr <- quantile(x[,"mpg"], c(.25, .75)) # inter-quartile-range
  whisker <- iqr+c(-1.5, 1.5)*diff(iqr)   # whiskers-range
  x[,"shape"] <- ifelse(x[,"mpg"] < whisker[1] | x[,"mpg"]>whisker[2], 1, 16)
  return(x)
})
# plot
p <- ggplot(df, aes(factor(cyl), mpg))
p + geom_boxplot() # without jittering
# adding shape manualy
p + geom_boxplot(outlier.size=-Inf) + 
  geom_jitter(aes(shape=factor(shape))) + 
  scale_shape_manual(guide=FALSE, values=c("16"=16, "1"=1)) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top