用c表示csv数据的表面图

时间:2012-09-27 15:15:42

标签: r lattice

我正在尝试用R做csv数据的三维表面图,但我不知道我究竟会这样做。 csv文件(sphere.csv)是

0,0,0,0,0,0,0,0,0,0,0
0,0,0,0.447213595,0.565685425,0.6,0.565685425,0.447213595,0,0,0
0,0,0.529150262,0.692820323,0.774596669,0.8,0.774596669,0.692820323,0.529150262,0,0
0,0.447213595,0.692820323,0.824621125,0.894427191,0.916515139,0.894427191,0.824621125,0.692820323,0.447213595,0
0,0.565685425,0.774596669,0.894427191,0.959166305,0.979795897,0.959166305,0.894427191,0.774596669,0.565685425,0
0,0.6,0.8,0.916515139,0.979795897,1,0.979795897,0.916515139,0.8,0.6,0
0,0.565685425,0.774596669,0.894427191,0.959166305,0.979795897,0.959166305,0.894427191,0.774596669,0.565685425,0
0,0.447213595,0.692820323,0.824621125,0.894427191,0.916515139,0.894427191,0.824621125,0.692820323,0.447213595,0
0,0,0.529150262,0.692820323,0.774596669,0.8,0.774596669,0.692820323,0.529150262,0,0
0,0,0,0.447213595,0.565685425,0.6,0.565685425,0.447213595,0,0,0
0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0

我能够像这样阅读

sphere_data <- read.csv (file="sphere.csv", head=FALSE, sep=",")

然后我尝试使用wireframe

library(lattice)
wireframe(z~x+y,sphere_data)

但这给了我一个错误。特别是,解释器似乎扼杀了z表达式的z~x+y。我不知道那个表达意味着什么,但我在互联网的其他例子中发现了它。

修改 根据 shujaa 的建议,dput(sphere_data)返回

structure(list(V1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), V2 = c(0, 0, 0, 0.447213595, 0.565685425, 0.6, 0.565685425,
0.447213595, 0, 0, 0, 0), V3 = c(0, 0, 0.529150262, 0.692820323,
0.774596669, 0.8, 0.774596669, 0.692820323, 0.529150262, 0, 0,
0), V4 = c(0, 0.447213595, 0.692820323, 0.824621125, 0.894427191,
0.916515139, 0.894427191, 0.824621125, 0.692820323, 0.447213595,
0, 0), V5 = c(0, 0.565685425, 0.774596669, 0.894427191, 0.959166305,
0.979795897, 0.959166305, 0.894427191, 0.774596669, 0.565685425,
0, 0), V6 = c(0, 0.6, 0.8, 0.916515139, 0.979795897, 1, 0.979795897,
0.916515139, 0.8, 0.6, 0, 0), V7 = c(0, 0.565685425, 0.774596669,
0.894427191, 0.959166305, 0.979795897, 0.959166305, 0.894427191,
0.774596669, 0.565685425, 0, 0), V8 = c(0, 0.447213595, 0.692820323,
0.824621125, 0.894427191, 0.916515139, 0.894427191, 0.824621125,
0.692820323, 0.447213595, 0, 0), V9 = c(0, 0, 0.529150262, 0.692820323,
0.774596669, 0.8, 0.774596669, 0.692820323, 0.529150262, 0, 0,
0), V10 = c(0, 0, 0, 0.447213595, 0.565685425, 0.6, 0.565685425,
0.447213595, 0, 0, 0, 0), V11 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("V1", "V2", "V3", "V4", "V5",
"V6", "V7", "V8", "V9", "V10", "V11"), class = "data.frame", row.names = c(NA,
-12L))

csv中的数据具有11x11值。这些值应该表示一个点的高度(比如z轴)。 csv中第一行的所有值都在同一个x轴上,对于第二行的值,直到最后一行的值都是如此。每行的第一个值在同一个y轴上,每个行的第二个值都是相同的,直到最后一行的值为止。

1 个答案:

答案 0 :(得分:2)

我只需使用

即可使用
sphere_data <- as.matrix(sphere_data)
wireframe(sphere_data)

希望有所帮助。