我经常在工作中使用箱线图,喜欢 ggplot2
美学。但标准geom_boxplot
缺少对我来说很重要的两件事: mustache 的末端和中间标签。多亏了这里的信息,我写了一个函数:
gBoxplot <- function(formula = NULL, data = NULL, font = "CMU Serif", fsize = 18){
require(ggplot2)
vars <- all.vars(formula)
response <- vars[1]
factor <- vars[2]
# A function for medians labelling
fun_med <- function(x){
return(data.frame(y = median(x), label = round(median(x), 3)))
}
p <- ggplot(data, aes_string(x = factor, y = response)) +
stat_boxplot(geom = "errorbar", width = 0.6) +
geom_boxplot() +
stat_summary(fun.data = fun_med, geom = "label", family = font, size = fsize/3,
vjust = -0.1) +
theme_grey(base_size = fsize, base_family = font)
return(p)
}
还有字体设置,不过这只是因为我懒得做主题了。下面是一个例子:
gBoxplot(hwy ~ class, mpg)
对我来说已经够用了,但是有一些限制(不能使用自动闪避等),最好根据
geom_boxplot
做一个新的geom会更好.我已阅读小插图 Extending ggplot2 ,但无法理解如何实现它。任何帮助将不胜感激。
最佳答案
所以一直在想这个问题。基本上,当你创建一个新的基元时,你通常会编写以下组合:
只有层功能需要对用户可见。你只需要写一个 stat-ggproto 如果您需要某种新方法来转换数据以制作原始数据。而你只需要写一个 geom-ggproto 如果您有一些新的基于网格的图形要创建。
在这种情况下,我们基本上是在堆肥 层功能已经存在了,我们真的不需要编写新的 ggprotos。写个新的就够了层功能 .此 层功能将创建您已经在使用的三个图层,并按照您想要的方式映射参数。在这种情况下:
geom_errorbar
和 stat_boxplot
– 获取我们的误差条 geom_boxplot
和 stat_boxplot
- 创建箱线图 geom_label
和 stat_summary
- 在框的中心创建具有平均值的文本标签。 当然你可以写一个新的 stat-ggproto 和一个新的 geom-ggproto 一次完成所有这些事情。或者也许你堆肥
stat_summary
和 stat_boxplot
合而为一,三者 geom-protos 同样,这是用一层来做到这一点的。但除非我们有效率问题,否则没有什么意义。无论如何,这是代码:
geom_myboxplot <- function(formula = NULL, data = NULL,
stat = "boxplot", position = "dodge",coef=1.5,
font = "sans", fsize = 18, width=0.6,
fun.data = NULL, fun.y = NULL, fun.ymax = NULL,
fun.ymin = NULL, fun.args = list(),
outlier.colour = NULL, outlier.color = NULL,
outlier.shape = 19, outlier.size = 1.5,outlier.stroke = 0.5,
notch = FALSE, notchwidth = 0.5,varwidth = FALSE,
na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE,...) {
vars <- all.vars(formula)
response <- vars[1]
factor <- vars[2]
mymap <- aes_string(x=factor,y=response)
fun_med <- function(x) {
return(data.frame(y = median(x), label = round(median(x), 3)))
}
position <- position_dodge(width)
l1 <- layer(data = data, mapping = mymap, stat = StatBoxplot,
geom = "errorbar", position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(na.rm = na.rm,
coef = coef, width = width, ...))
l2 <- layer(data = data, mapping = mymap, stat = stat, geom = GeomBoxplot,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(outlier.colour = outlier.colour, outlier.shape = outlier.shape,
outlier.size = outlier.size, outlier.stroke = outlier.stroke,
notch = notch, notchwidth = notchwidth, varwidth = varwidth,
na.rm = na.rm, ...))
l3 <- layer(data = data, mapping = mymap, stat = StatSummary,
geom = "label", position = position, show.legend = show.legend,
inherit.aes = inherit.aes, params = list(fun.data = fun_med,
fun.y = fun.y, fun.ymax = fun.ymax, fun.ymin = fun.ymin,
fun.args = fun.args, na.rm=na.rm,family=font,size=fsize/3,vjust=-0.1,...))
return(list(l1,l2,l3))
}
它允许您现在像这样创建自定义的箱线图:
ggplot(mpg) +
geom_myboxplot( hwy ~ class, font = "sans",fsize = 18)+
theme_grey(base_family = "sans",base_size = 18 )
它们看起来像这样:
备注 :我们实际上不必使用
layer
函数,我们可以使用原始 stat_boxplot
, geom_boxplot
, 和 stat_summary
在他们的位置调用。但是如果我们希望能够从我们的自定义箱线图中控制它们,我们仍然必须填写所有参数,所以我认为这样更清晰 - 至少从结构的角度而不是功能的角度来看.也许不是,这是一个品味问题......我也没有那种看起来更好看的字体。但我不想跟踪它并安装它。
关于r - 如何使用 ggproto 扩展 ggplot2 boxplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876683/