如何从分组数据中的点获得平滑的表面? [英] How to achieve smoothed surfaces from points in grouped data?

查看:21
本文介绍了如何从分组数据中的点获得平滑的表面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot(.) + geom_smooth(),我们可以通过带有分组数据的点获得漂亮的曲线.

With ggplot(.) + geom_smooth() we can achieve nice curves through points with grouped data.

library(ggplot2)
span <- 10

ggplot(data, aes(x = x, y = value, group = n)) +
  geom_smooth(aes(linetype = n), color = "blue", 
              se = FALSE)

现在我有了第三个维度,想在 3D 中可视化 value 和两个变量 var1, var2 之间的关系.我做了几次尝试,其中只有 car::scatter3d 使我更接近我想要的.但我在那里找不到平滑"选项,也没有保存情节的选项.

Now I have a third dimension and want to visualize the relation between value and the two variables var1, var2 in 3D. I made several attempts of which only car::scatter3d brought me more closely to what I want. But I couldn't find a "smooth" option there and also no option to save the plot.

library(car)
scatter3d(value ~ var1 + var2, data, 
          surface = FALSE, point.col = "blue",
          axis.ticks = TRUE, sphere.size = .8)

我也尝试过 rgl、plot3D、latticeplotly 包,但没有成功;从前两个我收到错误,从最后两个只是空网格.

I also tried rgl, plot3D, lattice and plotly package but with no success; from first two I'm receiving errors and from the last two just empty grids.

library(rgl)
persp3d(value ~ var1 + var2, data, col="skyblue")
# Error in seq.default(0, 1, len = nrow(z)) : 
#   argument 'length.out' must be of length 1

library(plot3D)
surf3D(as.matrix(data1[, 1]), as.matrix(data1[, 2]), as.matrix(data1[, 3]))
# Error in if (is.na(var)) ispresent <- FALSE else if (length(var) == 1) if (is.logic
# al(var)) if (!var) ispresent <- FALSE : 
#   argument is of length zero

library(lattice)
wireframe(value ~ var1 + var2, data)
# empty or wrong

library(plotly)
plot_ly(x = data$var1, y = data$value, z = data$var1, type = "surface")
# empty

如何在分组数据中实现具有第三维的平滑表面?我的目标是这样的(就像上面 ggplot() 中的组一样):

How is it possible to achieve smoothed surfaces with third dimension in grouped data? I'm aiming at something like this (just with the groups like in ggplot() above):

数据:

data <- structure(list(n = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                       3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                       3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                       3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                       4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                       4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4"
                                       ), class = "factor"), x = c(0, 0, 0, 0, 0, 0, 0.1, 0.1, 0.1, 
                                                                   0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 
                                                                   0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 
                                                                   0.5, 0, 0, 0, 0, 0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 
                                                                   0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 
                                                                   0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 
                                                                   0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 
                                                                   0.3, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 
                                                                   0.5, 0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 
                                                                   0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 
                                                                   0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5), 
                       y = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 
                             0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 
                             0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 
                             0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 
                             0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 
                             0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 
                             0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 
                             0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 
                             0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 
                             0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 
                             0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0, 0.1, 0.2, 
                             0.3, 0.4, 0.5), value = c(0, 0.000253671562082777, 0.00048064085447263, 
                                                       0.000680907877169559, 0.000854472630173565, 0.00100133511348465, 
                                                       0.000253671562082777, 0.00048064085447263, 0.000680907877169559, 
                                                       0.000854472630173565, 0.00100133511348465, 0.0011214953271028, 
                                                       0.00048064085447263, 0.000680907877169559, 0.000854472630173565, 
                                                       0.00100133511348465, 0.0011214953271028, 0.00121495327102804, 
                                                       0.000680907877169559, 0.000854472630173565, 0.00100133511348465, 
                                                       0.0011214953271028, 0.00121495327102804, 0.00128170894526035, 
                                                       0.000854472630173565, 0.00100133511348465, 0.0011214953271028, 
                                                       0.00121495327102804, 0.00128170894526035, 0.00132176234979973, 
                                                       0.00100133511348465, 0.0011214953271028, 0.00121495327102804, 
                                                       0.00128170894526035, 0.00132176234979973, 0.00133511348464619, 
                                                       0, 0.000126751167444963, 0.000240160106737825, 0.000340226817878586, 
                                                       0.000426951300867245, 0.000500333555703803, 0.000126751167444963, 
                                                       0.000240160106737825, 0.000340226817878586, 0.000426951300867245, 
                                                       0.000500333555703803, 0.000560373582388259, 0.000240160106737825, 
                                                       0.000340226817878586, 0.000426951300867245, 0.000500333555703803, 
                                                       0.000560373582388259, 0.000607071380920614, 0.000340226817878586, 
                                                       0.000426951300867245, 0.000500333555703803, 0.000560373582388259, 
                                                       0.000607071380920614, 0.000640426951300867, 0.000426951300867245, 
                                                       0.000500333555703803, 0.000560373582388259, 0.000607071380920614, 
                                                       0.000640426951300867, 0.000660440293529019, 0.000500333555703803, 
                                                       0.000560373582388259, 0.000607071380920614, 0.000640426951300867, 
                                                       0.000660440293529019, 0.00066711140760507, 0, 6.33544514838279e-05, 
                                                       0.000120040013337779, 0.000170056685561854, 0.000213404468156052, 
                                                       0.000250083361120373, 6.33544514838279e-05, 0.000120040013337779, 
                                                       0.000170056685561854, 0.000213404468156052, 0.000250083361120373, 
                                                       0.000280093364454818, 0.000120040013337779, 0.000170056685561854, 
                                                       0.000213404468156052, 0.000250083361120373, 0.000280093364454818, 
                                                       0.000303434478159386, 0.000170056685561854, 0.000213404468156052, 
                                                       0.000250083361120373, 0.000280093364454818, 0.000303434478159386, 
                                                       0.000320106702234078, 0.000213404468156052, 0.000250083361120373, 
                                                       0.000280093364454818, 0.000303434478159386, 0.000320106702234078, 
                                                       0.000330110036678893, 0.000250083361120373, 0.000280093364454818, 
                                                       0.000303434478159386, 0.000320106702234078, 0.000330110036678893, 
                                                       0.000333444481493831, 0, 1.26675111674112e-05, 2.40016001066738e-05, 
                                                       3.40022668177879e-05, 4.26695113007534e-05, 5.00033335555704e-05, 
                                                       1.26675111674112e-05, 2.40016001066738e-05, 3.40022668177879e-05, 
                                                       4.26695113007534e-05, 5.00033335555704e-05, 5.60037335822388e-05, 
                                                       2.40016001066738e-05, 3.40022668177879e-05, 4.26695113007534e-05, 
                                                       5.00033335555704e-05, 5.60037335822388e-05, 6.06707113807587e-05, 
                                                       3.40022668177879e-05, 4.26695113007534e-05, 5.00033335555704e-05, 
                                                       5.60037335822388e-05, 6.06707113807587e-05, 6.40042669511301e-05, 
                                                       4.26695113007534e-05, 5.00033335555704e-05, 5.60037335822388e-05, 
                                                       6.06707113807587e-05, 6.40042669511301e-05, 6.60044002933529e-05, 
                                                       5.00033335555704e-05, 5.60037335822388e-05, 6.06707113807587e-05, 
                                                       6.40042669511301e-05, 6.60044002933529e-05, 6.66711114074272e-05
                             )), .Names = c("n", "x", "y", "value"), row.names = c(NA, 
                                                                                   -144L), class = "data.frame")

推荐答案

你的数据在 n 的每一层看起来像一个网格,所以在 rgl 中你可以强制xyvalue 列到矩阵并使用 persp3d:

Your data looks like a grid within each level of n, so in rgl you could coerce the x, y, and value columns to matrices and use persp3d:

library(rgl)
open3d()
for (level in unique(data$n)) {
  sub <- subset(data, n == level)
  x <- matrix(sub$x, 6,6)
  y <- matrix(sub$y, 6,6)
  value <- matrix(sub$value, 6,6)
  persp3d(x, y, value, col = level, alpha = 0.5, add = level > 1)
}

(存在 add = level > 1 行,以便将第一个之后的级别添加到同一图中.)

(The add = level > 1 line is there so that levels after the first one are added to the same plot.)

如果您的数据不一定在网格中,您仍然可以绘制曲面,但需要做更多的工作.您需要 deldir 包来对数据进行三角测量.例如,

If your data isn't necessarily in a grid, you can still plot the surfaces, but it's a bit more work. You need the deldir package to triangulate your data. For example,

library(rgl)
library(deldir)
open3d()
for (level in unique(data$n)) {
  sub <- subset(data, n == level)
  surf <- deldir(sub$x, sub$y, z = sub$value, suppressMsge = TRUE)
  persp3d(surf, col = level, alpha = 0.5, add = TRUE)
}
aspect3d(1,1,1)
decorate3d(zlab = "value")

最后需要 aspect3ddecorate3d 调用,因为 persp3d.deldir 不会自动设置纵横比或设置自定义轴标签.这给

You need the aspect3d and decorate3d calls at the end because persp3d.deldir doesn't automatically set the aspect ratio or set custom axis labels. This gives

这篇关于如何从分组数据中的点获得平滑的表面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆