我正在尝试使用Plotly包在同一个图表上绘制多个曲面,但我无法完成它。几天前我可以使用相同的代码毫无问题地做到这一点,但现在似乎在Plotly上更新之后它已经不可能了。这是我正在尝试做的一个例子:
#Volcano surface
plot_ly(z = volcano,
type = 'surface') %>%
#First rectangle
add_trace(x = c(10, 60),
y = c(10, 50),
z = matrix(160, nrow = 2, ncol = 2),
type = 'surface', showscale = FALSE) %>%
#Second rectangle
add_trace(x = c(10, 60),
y = c(10, 50),
z = matrix(180, nrow = 2, ncol = 2),
type = 'surface',
showscale = FALSE)
当我运行上面的代码时,我得到以下输出:
Error in p$x$data[[idx]]$marker :
$ operator is invalid for atomic vectors
如果我将Plotly对象分配给某个变量而不是使用'%>%'运算符,如下所示:
#Volcano surface
p <- plot_ly(z = volcano,
type = 'surface')
#First rectangle
p <- add_trace(p,
x = c(10, 60),
y = c(10, 50),
z = matrix(160, nrow = 2, ncol = 2),
type = 'surface', showscale = FALSE)
#Second rectangle
p <- add_trace(p,
x = c(10, 60),
y = c(10, 50),
z = matrix(180, nrow = 2, ncol = 2),
type = 'surface',
showscale = FALSE)
#Plot object
p
...然后我得到与以前相同的输出。
如果我绘制火山和其中一个矩形,它就可以正常工作。此外,我试图绘制不同于矩形的表面,但仍然会得到相同的错误。
以下是一些可能有用的信息:
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS
locale:
[1] LC_CTYPE=pt_BR.UTF-8 LC_NUMERIC=C LC_TIME=pt_BR.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=pt_BR.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=pt_BR.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_4.5.2 ggplot2_2.1.0 shiny_0.14.1 doMC_1.3.4
[5] iterators_1.0.8 quantstrat_0.9.1739 foreach_1.4.3 blotter_0.9.1741
[9] PerformanceAnalytics_1.4.4000 FinancialInstrument_1.2.0 quantmod_0.4-6 TTR_0.23-1
[13] xts_0.9-7 zoo_1.7-13
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 compiler_3.3.1 plyr_1.8.4 base64enc_0.1-3 tools_3.3.1 digest_0.6.10 viridisLite_0.1.3
[8] jsonlite_1.1 tibble_1.2 gtable_0.2.0 lattice_0.20-34 DBI_0.5-1 yaml_2.1.13 dplyr_0.5.0
[15] httr_1.2.1 htmlwidgets_0.7 grid_3.3.1 R6_2.1.3 purrr_0.2.2 tidyr_0.6.0 magrittr_1.5
[22] scales_0.4.0 codetools_0.2-14 htmltools_0.3.5 assertthat_0.1 mime_0.5 xtable_1.8-2 colorspace_1.2-6
[29] httpuv_1.3.3 lazyeval_0.2.0 munsell_0.4.3
关于这个问题的任何想法?
答案 0 :(得分:1)
解决。重写代码:
#Create Plotly object
plot_ly(showscale = FALSE) %>%
#Volcano surface
add_surface(z = volcano) %>%
#First rectangle
add_surface(x = c(10, 60),
y = c(10, 50),
z = matrix(160, nrow = 2, ncol = 2)) %>%
#Second rectangle
add_surface(x = c(10, 60),
y = c(10, 50),
z = matrix(180, nrow = 2, ncol = 2))
像魅力一样工作。它是基于this多个曲面图示例。