【问题标题】:How to plot an informative stratified surface graph?如何绘制信息丰富的分层曲面图?
【发布时间】:2018-07-29 23:55:38
【问题描述】:

我想绘制按 n 分层的 3D 数据 d1。感谢this answer,到目前为止,我使用rgl 实现了以下解决方案,虽然我找不到将情节旋转到最佳视角的方法,但它看起来已经不错了。在这方面更直接的似乎是plotly,可以用鼠标旋转图形。后者也已经有了我需要的颜色和相应的图例。虽然在这两种方式中我都不明白如何保存图形。如何将它编织成我打算做的 rmarkdown。我不依赖特定的包,只想要以下内容:

  • 地层
  • 带有图例的颜色/纹理阴影
  • 可保存,可编织

这是我的尝试,数据如下。

car::some(d1, 5)
#        n   x   y        value
# 37  1000 0.0 0.0 0.000000e+00
# 93  2000 0.3 0.2 2.500834e-04
# 101 2000 0.4 0.4 3.201067e-04
# 111 4000 0.0 0.2 2.400160e-05
# 142 4000 0.5 0.3 6.400427e-05

# change levels
levels <- levels(d1$n)  # preserve for later
d1$n <- as.factor(as.numeric(d1$n))

# arrayer
arrayIt <- function(x){
  # makes array of d1 by x, y, z
  d <- sqrt(nrow(x)/length(unique(x[, 1])))  # dim. of matrices
  ar <- array(NA, c(d, d, 3))  # init. array
  ar[, , 1] <- matrix(x[, 2], d, d)  # x
  ar[, , 2] <- matrix(x[, 3], d, d)  # y
  ar[, , 3] <- matrix(x[, 4], d, d)  # z
  return(ar)
}

# list of 4 arrays for each  n
ls1 <- lapply(seq_along(unique(d1[, 1])), function(n) arrayIt(d1[d1[, 1] == n, ]))

# plot
library(rgl)
lapply(seq_along(unique(d1$n)), function(i){
  x <- ls1[[i]][,,1]
  y <- ls1[[i]][,,2]
  z <- ls1[[i]][,,3]
  persp3d(x, y, z, col = i, alpha = .5, add = i > 1)  # MARK
})

例如plotly 已经提供了颜色阴影和图例,这非常有用,但是 plot_ly 在上面的 lapply 中不起作用,我也没有找到如何添加层的选项。

library(plotly)
plot_ly(x=x, y=y, z=z, type = "surface")  # inserted at MARK didn't work

总之,想要的结果应该是两个图的交集。

数据:

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("500", "1000", "2000", 
                                                                               "4000"), 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")

【问题讨论】:

  • 不确定为什么轮换对您不起作用。你需要什么输出格式?
  • @user2554330 查看我对您的回答的评论
  • 这个我没有答案...但是如果你想要 PDF 输出,这很难。查看器不太支持 PDF 中的交互式图形。您可以尝试rgl::writeASY,但可能会令人失望。

标签: r plot 3d plotly rgl


【解决方案1】:

您需要在 plotly 中使用add_surface()...并且您的数据需要为曲面图的矩阵形式。 plotly 网站为各种情节提供了不错的文档。 https://plot.ly/r/3d-surface-plots/。在plotly 命令中只获取一个色标集showscale==FALSE,然后为其中一个图重新打开它。

z <- lapply(unique(data$n), 
            function(x) as.matrix(reshape(data[data$n==x,-1], 
                                          idvar = "x", timevar = "y", direction = "wide") ))

plot_ly(showscale=FALSE) %>% add_surface(z=~z[[1]][, -1], cmin=min(data$value), cmax=max(data$value), showscale=TRUE) %>%
  add_surface(z = ~z[[2]][, -1], cmin=min(data$value), cmax=max(data$value)) %>%
  add_surface(z=~z[[3]][,-1], cmin=min(data$value), cmax=max(data$value)) %>%
  add_surface(z~z[[4]][,-1], cmin=min(data$value), cmax=max(data$value))

未设置 cmin cmax 的旧情节

设置了 cmincmax 的新图,以便 z 值在所有层中的颜色一致

【讨论】:

  • 看起来很有说服力,虽然我无法让图例出现,例如通过`%>%布局(showlegend = TRUE)`。我想用 rmarkdown 编织成 pdf(通常静态图被编织成 pdf)。我只是在尝试,但似乎并不那么容易,尽管 HTML 可以完美运行。是否可以指定特定视角的值并绘制静态图?
  • 看看这个关于用情节图形编织 pdf 的帖子:github.com/ropensci/plotly/issues/889
  • 产量警告消息:'layout' 对象没有这些属性:'showscale',没有图例
  • 请注意我在帖子中设置showscale 的位置。尝试将其更改为 TRUE。如果您只想要一个色标,则需要在每个 add_surface 中设置 showscale==FALSE
  • 啊,你说得对,对不起,我没注意到!很好的答案谢谢!
【解决方案2】:

正如您的数据的不同视图 - 因为单个 3D 图中的四个表面看起来有点太忙了 - 您可以尝试在 value 上使用等高线和着色。 (我不确定您需要强调/分析数据集的哪些特定方面。)

library(tidyverse)
library(viridis)

data %>%
  ggplot(aes(x, y)) +
  geom_raster(aes(fill = value), interpolate = T) +
  geom_contour(aes(z = value), bins = 15) +
  facet_wrap(~ n, nrow = 1) +
  scale_fill_viridis()

【讨论】:

  • 确实很有趣,虽然我需要其他版本。但我会牢记这一点,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 2022-10-31
相关资源
最近更新 更多