我想在 R 中的单个绘图上绘制 60,000 多个非重叠三角形(非结构化三角形网格的一部分)。目前,每个绘图需要 15-20 分钟,这使得无法使用它来制作动画。例如,
n <- 100 #Except replace this with 60,000
x <- matrix(runif(3*n), n)
y <- matrix(runif(3*n), n)
cols <- heat.colors(n)[order(x[,1])]
poly <- function(i) {polygon(x[i,], y[i,], col=cols[i])}
plot(0, xlim=c(min(x),max(x)), ylim=c(min(y),max(y)))
sapply(1:n, poly)
是否可以在每个多边形之后抑制多边形()重绘?我猜这是最耗时的步骤,手册页中没有提到。关于如何实现这一点的替代建议将不胜感激。谢谢你。
最佳答案
您可以将多个多边形传递给 polygon
.您所要做的就是与 NA
分开.这是一个代码:
cuts <- function(x)
{
n <- length(x) %/% 3
map <- rep(c(TRUE,TRUE,TRUE,FALSE), n)
result <- rep(NA, n*4)
result[map] <- x
result
}
set.seed(1234)
n <- 10000
x <- matrix(runif(3*n), n)
y <- matrix(runif(3*n), n)
cols <- heat.colors(n)[order(x[,1])]
plot(0, xlim=c(min(x),max(x)), ylim=c(min(y),max(y)))
polygon(x=cuts(t(x)), y=cuts(t(y)), col=cols)
工作很快。我已经测试了控制种子并与您的代码生成的图进行比较。
关于r - 在 R 中调用多边形时禁止重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534032/