This solution will be quite long. The problem is that when position="jitter"you cannot get the exact coordinates for the points, so you need to find a workaround.
, ggplot_build(). . group outliers, , ggplot . .
p <- ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot(outlier.shape=10, outlier.size=8) +
geom_point(aes(color=mpg), position="jitter", size=4)
gg<-ggplot_build(p)
gg$data[[1]]
ymin lower middle upper ymax outliers notchupper notchlower x PANEL group weight ymin_final
1 21.4 22.80 26.0 30.40 33.9 29.62055 22.37945 1 1 1 1 21.4
2 17.8 18.65 19.7 21.00 21.4 21.10338 18.29662 2 1 2 1 17.8
3 13.3 14.40 15.2 16.25 18.7 10.4, 10.4, 19.2 15.98120 14.41880 3 1 3 1 10.4
ymax_final xmin xmax
1 33.9 0.625 1.375
2 21.4 1.625 2.375
3 19.2 2.625 3.375
xx<-gg$data[[1]][c("group","outliers")]
xx
group outliers
1 1
2 2
3 3 10.4, 10.4, 19.2
group 4,6 8, , cyl.
xx$group<-c(4,6,8)
mtcars . , , mpg outliers cyl. (TRUE FALSE) out.
mtcars.new<-merge(mtcars,xx,by.x="cyl",by.y="group")
mtcars.new$out<-apply(mtcars.new,1,function(x) x$mpg %in% x$outliers)
. geom_boxplot(). out, . scale_shape_manual() scale_size_manual() .
ggplot(mtcars.new, aes(factor(cyl), mpg)) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(color=mpg,shape=out,size=out), position="jitter")+
scale_shape_manual(values=c(16,10),guide="none")+
scale_size_manual(values=c(4,8),guide="none")
