dcl*_*dcl 36 graphics plot r strange-attractor
是plot()
在R键曲线100万个左右的数据点的最有效方法是什么?我想画一堆这些Clifford吸引者.这是一个我从一个非常大的图像缩小的例子:
这是我用于绘制非常大的8K(7680x4320)图像的一些代码的链接.
生成50或1亿个点(使用Rcpp)不需要很长时间,也不需要获取颜色+透明度的十六进制值,但实际绘图和保存到磁盘的速度非常慢.
编辑:使用的代码
# Load packages
library(Rcpp)
library(viridis)
# output parameters
output_width = 1920 * 4
output_height = 1080 * 4
N_points = 50e6
point_alpha = 0.05 #point transperancy
# Attractor parameters
params <- c(1.886,-2.357,-0.328, 0.918)
# C++ function to rapidly generate points
cliff_rcpp <- cppFunction(
"
NumericMatrix cliff(int nIter, double A, double B, double C, double D) {
NumericMatrix x(nIter, 2);
for (int i=1; i < nIter; ++i) {
x(i,0) = sin(A*x(i-1,1)) + C*cos(A*x(i-1,0));
x(i,1) = sin(B*x(i-1,0)) + D*cos(B*x(i-1,1));
}
return x;
}"
)
# Function for mapping a point to a colour
map2color <- function(x, pal, limits = NULL) {
if (is.null(limits))
limits = range(x)
pal[findInterval(x,
seq(limits[1], limits[2], length.out = length(pal) + 1),
all.inside = TRUE)]
}
# Obtain matrix of points
cliff_points <- cliff_rcpp(N_points, params[1], params[2], params[3], params[4])
# Calculate angle between successive points
cliff_angle <- atan2(
(cliff_points[, 1] - c(cliff_points[-1, 1], 0)),
(cliff_points[, 2] - c(cliff_points[-1, 2], 0))
)
# Obtain colours for points
available_cols <-
viridis(
1024,
alpha = point_alpha,
begin = 0,
end = 1,
direction = 1
)
cliff_cols <- map2color(
cliff_angle,
c(available_cols, rev(available_cols))
)
# Output image directly to disk
jpeg(
"clifford_attractor.jpg",
width = output_width,
height = output_height,
pointsize = 1,
bg = "black",
quality = 100
)
plot(
cliff_points[-1, ],
bg = "black",
pch = ".",
col = cliff_cols
)
dev.off()
Run Code Online (Sandbox Code Playgroud)
我最近发现了R 的Scattermore包,它比 R 的标准绘图函数快一个数量级。scattermoreplot()
绘制 100m 个具有颜色和透明度的点大约需要 2 分钟,而plot()
大约需要半小时。
归档时间: |
|
查看次数: |
1270 次 |
最近记录: |