为给定的 df 绘制几何矢量图并生成 .png
draw geometric vector diagrams for given df and generate .png
使用 matlib
包我能够绘制几何矢量图。
使用以下代码可以为给定坐标绘制 3d 矢量
library(matlib)
library(rgl)
vec <- rbind(diag(3), c(1,1,1))
rownames(vec) <- c("X", "Y", "Z", "J")
open3d()
vectors3d(vec, col=c(rep("black",3), "red"), lwd=2)
但是,当我想提供来自 df
的坐标时
set.seed(12)
x <- runif(10,-0.14,0.1)
y <- runif(10,-0.14,0.1)
z <-sort(runif(10,-0.9,0.9),decreasing=TRUE)
df <- data.frame(x,y,z)
编写类似给出错误的代码;
vec <- rbind(diag(3), c(df[1,]))
vectors3d(vec, col=c(rep("black",3), "red"), lwd=2)
Error in ends - starts : non-numeric argument to binary operator
那么,问题是我们如何将df
到vectors3d
的每一行逐一提供来创建.png图片呢?谢谢!
我们需要 unlist
行,即 df[1,]
。它仍然是具有 1 行的 data.frame
。
vec <- rbind(diag(3), unlist(df[1,]))
str(vec)
# num [1:4, 1:3] 1 0 0 -0.123 0 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:3] "x" "y" "z"
由于 OP 使用 c
然后 rbind
,它创建了一个 list
列
vec <- rbind(diag(3), c(df[1,]))
str(vec)
#List of 12
# $ : num 1
# $ : num 0
# $ : num 0
# $ : num -0.123
# $ : num 0
# $ : num 1
# $ : num 0
# $ : num -0.0458
# $ : num 0
# $ : num 0
# $ : num 1
# $ : num 0.518
# - attr(*, "dim")= int [1:2] 4 3
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:3] "x" "y" "z"
修复后,剧情应该可以了。
使用 matlib
包我能够绘制几何矢量图。
使用以下代码可以为给定坐标绘制 3d 矢量
library(matlib)
library(rgl)
vec <- rbind(diag(3), c(1,1,1))
rownames(vec) <- c("X", "Y", "Z", "J")
open3d()
vectors3d(vec, col=c(rep("black",3), "red"), lwd=2)
但是,当我想提供来自 df
set.seed(12)
x <- runif(10,-0.14,0.1)
y <- runif(10,-0.14,0.1)
z <-sort(runif(10,-0.9,0.9),decreasing=TRUE)
df <- data.frame(x,y,z)
编写类似给出错误的代码;
vec <- rbind(diag(3), c(df[1,]))
vectors3d(vec, col=c(rep("black",3), "red"), lwd=2)
Error in ends - starts : non-numeric argument to binary operator
那么,问题是我们如何将df
到vectors3d
的每一行逐一提供来创建.png图片呢?谢谢!
我们需要 unlist
行,即 df[1,]
。它仍然是具有 1 行的 data.frame
。
vec <- rbind(diag(3), unlist(df[1,]))
str(vec)
# num [1:4, 1:3] 1 0 0 -0.123 0 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:3] "x" "y" "z"
由于 OP 使用 c
然后 rbind
,它创建了一个 list
列
vec <- rbind(diag(3), c(df[1,]))
str(vec)
#List of 12
# $ : num 1
# $ : num 0
# $ : num 0
# $ : num -0.123
# $ : num 0
# $ : num 1
# $ : num 0
# $ : num -0.0458
# $ : num 0
# $ : num 0
# $ : num 1
# $ : num 0.518
# - attr(*, "dim")= int [1:2] 4 3
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:3] "x" "y" "z"
修复后,剧情应该可以了。