抖动如果ggplot2 boxplot中有多个异常值 [英] jitter if multiple outliers in ggplot2 boxplot
问题描述
现在我倾向于一个没有胡须的boxplot,只显示1,2。和3.四分位数+数据点多于然后例如1人口SD +/-样本中位数。
然而,我需要向一些教师展示这张图表,以便衡量他们最喜欢的东西。我想比较我的图和普通的箱子图。但是,如果仅存在一个异常值,则普通盒图看起来是相同的,或者例如5个异常值相同。在这种情况下,这将是一个交易断路器。
例如
test <-structure(list(value = c(3,5,3,3,6,4,5,4,6,4,6,4,
4,6,5,3,3 ,4,4,4,4,4,4,4,4,4,5,6,6,4,3,5,4,
,5,6,6,4,5,5,3, ,4,4,6,4,4,5,5,3,4,5,8,8,8,8,
9,6,6,7,6,9),位置=结构( (1L,2L,1L,1L,1L,2L,1L,1L,2L,2L,1L,2L,1L,2L,1L,2L,2L,2L,
2L,1L,1L,1L,1L,2L,1L,2L,1L,2L,2L,2L,1L,2L,1L,2L,
2L,1L,1L,2L,2L, 1L,1L,1L,1L,2L,1L,1L,2L,2L,2L,1L,
1L,2L,2L,1L,2L,1L),标签= c(a,b ),class =factor)),.Names = c(value,
places),row.names = c(NA,-60L),class =data.frame)
ggplot(test,aes(x = places,y = value))+ geom_boxplot()
这里有两个异常值(a,9) - 但只显示一个点。
所以我的问题是:如何抖动异常值。并且 - 你会为这类数据建议什么样的显示? 您可以重新定义功能
GeomBoxplot $ draw <-function(。,data,...,outlier.colour =black,outlier.shape = 16,
{
defaults < - with(data,data.frame(x = x,xmin = xmin,xmax = xmax,
color = color,size = size,linetype = 1,group = 1,
alpha = 1,fill = alpha(fill,alpha),stringsAsFactors = FALSE))
defaults2< - defaults [c (数据$离群值)&长度(数据$离群值[[1]]> =
1)){$ b $ (data_frame(x = data $ x [rep(1,length(data_outterlier.jitter,height = 0)bpp< -position_jitter ]))]],y = data $ outliers [[1]]),scale)
outlier_grob< - GeomPoint $ draw(data.frame(x = p $ x,y = p $ y,color =我(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 = GeomRect $ draw(data.frame(ymax = middle,
ymin = middle,defaults),...))))
}
ggplot(test,aes(x = places,y = value))+ geom_boxplot(outlier.jitter = 0.05)
这是临时解决方案。当然,就OOP而言,您应该创建一个GeomBoxplot的子类并覆盖该函数。这很容易,因为ggplot2很好。
===为子类定义添加例子===
< GeomBoxplotJitterOutlier< - proto(GeomBoxplot,{
draw< - function(。,data,...,outlier.colour =black,outlier.shape =
outlier.size = 2,outlier.jitter = 0){
#复制上面的函数'draw'的主体并粘贴到这里。
}
objname< - boxplot_jitter_outlier
desc< - 带有抖动离群值的框和胡须图
guide_geom< - function(。)boxplot_jitter_outlier
})
geom_boxplot_jitter_outlier< - GeomBoxplotJitterOutlier $ build_accessor()
然后您可以使用您的子类:
ggplot(test,aes(x = places,y = value))+ geom_boxplot_jitter_outlier(outlier.jitter = 0.05)
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).
Right now i am leaning towards a whisker-less boxplot, showing only 1.,2. and 3. quartile + datapoints more then e.g. 1 population SD +/- the sample median.
This I can do.
However - I need to show this graph to some teachers, in order to gauge what they like most. I'd like to compare my graph with a normal boxplot. But the normal boxplot looks the same if there is only one outlier, or e.g. 5 outliers at the same value. In this case this would be a deal-breaker.
e.g.
test <-structure(list(value = c(3, 5, 3, 3, 6, 4, 5, 4, 6, 4, 6, 4,
4, 6, 5, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 5, 6, 6, 4, 3, 5, 4,
6, 5, 6, 4, 5, 5, 3, 4, 4, 6, 4, 4, 5, 5, 3, 4, 5, 8, 8, 8, 8,
9, 6, 6, 7, 6, 9), places = structure(c(1L, 2L, 1L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 2L, 1L), .Label = c("a", "b"), class = "factor")), .Names = c("value",
"places"), row.names = c(NA, -60L), class = "data.frame")
ggplot(test, aes(x=places,y=value))+geom_boxplot()
Here there are two outliers at ("a",9) - but only one "dot" shown.
So my question: How to jitter the outliers. And - what kind of display would you suggest for this kind of data?
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)
这篇关于抖动如果ggplot2 boxplot中有多个异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!