如何使用 ggproto 扩展 ggplot2 boxplot?
How to extend ggplot2 boxplot with ggproto?
我经常在工作中使用箱线图并且喜欢 ggplot2
美学。但是标准 geom_boxplot
缺少对我来说很重要的两件事:胡须的末端和中间标签。感谢这里的信息,我写了一个函数:
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,但无法理解如何实现它。
任何帮助将不胜感激。
所以一直在考虑这个问题。基本上当你创建一个新的基元时,你通常会写一个组合:
- 一个层函数
- A stat-ggproto,
- A geom-ggproto
只有 layer-function 需要对用户可见。如果你需要一些新的方法来转换你的数据来制作你的原语,你只需要写一个 stat-ggproto 。如果你要创建一些新的基于网格的图形,你只需要写一个 geom-ggproto。
在这种情况下,我们基本上是在堆肥 layer-function 已经存在的地方,我们真的不需要编写新的 ggprotos。写一个新的layer-function就够了。此 layer-function 将创建您已经在使用的三个图层,并按照您想要的方式映射参数。在这种情况下:
- 第 1 层 – 使用
geom_errorbar
和 stat_boxplot
– 获取我们的误差线
- Layer2 – 使用
geom_boxplot
和 stat_boxplot
- 创建箱线图
- 第 3 层 – 用户
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
代替他们打电话。但是如果我们希望能够从我们的自定义箱线图中控制它们,我们仍然必须填写所有参数,所以我认为这样更清楚 - 至少从结构的角度来看,而不是功能.也许不是,这是一个品味问题...
我也没有那种看起来确实好很多的字体。但是我不想追踪并安装它。
我经常在工作中使用箱线图并且喜欢 ggplot2
美学。但是标准 geom_boxplot
缺少对我来说很重要的两件事:胡须的末端和中间标签。感谢这里的信息,我写了一个函数:
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,但无法理解如何实现它。
任何帮助将不胜感激。
所以一直在考虑这个问题。基本上当你创建一个新的基元时,你通常会写一个组合:
- 一个层函数
- A stat-ggproto,
- A geom-ggproto
只有 layer-function 需要对用户可见。如果你需要一些新的方法来转换你的数据来制作你的原语,你只需要写一个 stat-ggproto 。如果你要创建一些新的基于网格的图形,你只需要写一个 geom-ggproto。
在这种情况下,我们基本上是在堆肥 layer-function 已经存在的地方,我们真的不需要编写新的 ggprotos。写一个新的layer-function就够了。此 layer-function 将创建您已经在使用的三个图层,并按照您想要的方式映射参数。在这种情况下:
- 第 1 层 – 使用
geom_errorbar
和stat_boxplot
– 获取我们的误差线 - Layer2 – 使用
geom_boxplot
和stat_boxplot
- 创建箱线图 - 第 3 层 – 用户
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
代替他们打电话。但是如果我们希望能够从我们的自定义箱线图中控制它们,我们仍然必须填写所有参数,所以我认为这样更清楚 - 至少从结构的角度来看,而不是功能.也许不是,这是一个品味问题...
我也没有那种看起来确实好很多的字体。但是我不想追踪并安装它。