autoplot.microbenchmark 实际绘制了什么?

2024-05-21

根据文档,microbenchmark:::autoplot“使用 ggplot2 生成更清晰的微基准计时图。”

凉爽的!让我们尝试一下示例代码:

library("ggplot2")
tm <- microbenchmark(rchisq(100, 0),
                     rchisq(100, 1),
                     rchisq(100, 2),
                     rchisq(100, 3),
                     rchisq(100, 5), times=1000L)
autoplot(tm)

我在文档中没有看到任何关于......柔软的波动的信息,但我的最佳猜测是函数创建者的这个答案 https://stackoverflow.com/a/6871968/2023432这就像一系列平滑的运行时间箱线图,上四分位数和下四分位数连接在形状的主体上。或许?这些情节看起来太有趣了,不去看看这里发生了什么。

这是什么剧情?


简短的答案是小提琴情节 https://en.wikipedia.org/wiki/Violin_plot:

它是一个箱形图,每侧都有一个旋转的核密度图。


答案越长越有趣(?)。当您致电autoplot函数,你实际上是在调用

## class(ts) is microbenchmark
autoplot.microbenchmark

然后我们可以通过检查实际的函数调用

R> getS3method("autoplot", "microbenchmark")
function (object, ..., log = TRUE, y_max = 1.05 * max(object$time)) 
{
    y_min <- 0
    object$ntime <- convert_to_unit(object$time, "t")
    plt <- ggplot(object, ggplot2::aes_string(x = "expr", y = "ntime"))
 ## Another ~6 lines or so after this

关键行是+ stat_ydensity()。看着?stat_ydensity你 来到小提琴图的帮助页面。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

autoplot.microbenchmark 实际绘制了什么? 的相关文章

随机推荐