我有一个R
/ggplot2
似乎需要的用例geom_raster
:在 x、y 位置具有 z 值的常规笛卡尔网格。我一直在使用geom_tile
,并且我期望从切换到geom_raster
。但我好像没看到人...
这是一个玩具示例(但大小合适),使用base
图形:
n <- m <- 200
x <- 1:n
y <- 1:m
f <- function(x, y) 10 * sin(x / n) * cos(y / m)
z <- outer(x, y, f)
system.time(image(z))
user system elapsed
0.998 0.007 1.023
这是与ggplot2
:
obs <- expand.grid(x=x, y=y)
obs$z <- as.numeric(as.list(z))
require(ggplot2)
p <- ggplot(obs, aes(x=x, y=y, fill=z))
system.time(show(p + geom_tile()))
user system elapsed
7.328 0.891 8.187
require(ggExtra)
system.time(show(p + geom_raster()))
user system elapsed
7.000 0.637 7.799
所以,虽然收益不大,但远没有达到我的预期。我做错了吗?提前谢谢了!
你应该使用geom_raster
从最新的ggplot2
(开发版本 https://github.com/hadley/ggplot2/wiki/Developing-ggplot2-using-github,目前),而不是有缺陷的原型ggExtra
(顺便说一句,这个包现在已被弃用)。
这样做,我得到了更好的结果,4.705 vs. 1.416(已过去)。相当大的进步。
编辑:事实证明?geom_raster
in ggplot2
已经在我的系统上提供了更好的基准
benchplot(base + geom_raster())
step user.self sys.self elapsed
1 construct 0.006 0.004 0.010
2 build 0.887 0.212 1.109
3 render 0.449 0.119 0.584
4 draw 0.108 0.005 0.141
5 TOTAL 1.450 0.340 1.844
> benchplot(base + geom_tile())
step user.self sys.self elapsed
1 construct 0.016 0.005 0.026
2 build 1.031 0.329 1.365
3 render 1.021 0.297 1.318
4 draw 0.987 0.041 1.040
5 TOTAL 3.055 0.672 3.749
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)