在Plotly中为3D散点图添加注释

3
我是一个新手,希望能得到一些帮助。我刚开始使用Plotly,并使用它构建了一个非常基本的三维散点图,模拟了一些货架和物品的位置。虽然我已经建立了“货架”,并且绘制出了图形,但我想添加一个注释来指示我正在查看哪个货架。
这是我的程序当前的输出这是我想要实现的效果

我尝试查阅文档并尝试添加注释(就像我为二维类似物做的那样),虽然代码编译了,但文本没有显示出来。

p <- plot_ly(all, x = X, y = Y, z = Z,  
    text = paste0("Candy: ", SKU,"; QTY: ", Qty), 
    color = OccupiedPct, 
    colors = cols,
    hoverinfo = "text",
    type = "scatter3d", mode = "markers") %>%
layout(
   title = "Shelves"
)
sub <- list(
    x = 3,
    y = 2,
    z = 0,
    text = "Shelf1",
    xref = "x",
    yref = "y",
    zref = "z",
    showarrow = FALSE
)
p %>%
   layout(annotations = sub)

谢谢您的提前帮助!Mike 更新 感谢@MLavoie的回答,很抱歉没有提供可重复的数据集。不幸的是,我无法复制您所做的操作。我注意到您绘图的z轴设置为零,我尝试重置我的也没成功。然而,即使调整点在图形范围内,它仍然无法显示出来。这是我得到的图: Output 当我运行这段代码时
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]

p <- plot_ly(d, x = carat, y = price, z=depth, text = paste("Clarity: ",     clarity),
         type="scatter3d", mode="markers")

p %>% 
  add_trace(x = 2.5, y = 5000, z = 56, type = "scatter3d", text = "test", mode = "text") %>% 
  add_trace(x = 2.5, y = 15000, z = 56, type = "scatter3d", text = "test", mode = "text") %>% 
  layout(
    xaxis = list(range = c(0, 5)),
    yaxis = list(range(0,20000)),
    zaxis = list(range(0,100)),
    showlegend = FALSE)

为了完整起见,这是我的会话信息:

R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_2.0.16 ggplot2_2.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3      digest_0.6.9     grid_3.2.1       plyr_1.8.3       R6_2.1.2         jsonlite_0.9.19  gtable_0.1.2     magrittr_1.5     scales_0.3.0     httr_1.1.0      
[11] viridis_0.3.2    tools_3.2.1      htmlwidgets_0.5  munsell_0.4.2    yaml_2.1.13      base64enc_0.1-3  colorspace_1.2-6 htmltools_0.3    gridExtra_2.0.0 

再次感谢!


你能提供一个可重现的例子吗? - MLavoie
非常抱歉我没有提供那个!我看到你已经回答了这个问题,所以我会测试一下,尽管它看起来像是答案。再次感谢@Mlavoie,对于我的帖子不完整表示抱歉。 - MVWyck
1个回答

2
使用鸢尾花数据集(您没有提供可复制的示例),您可以尝试以下操作:
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]

p <- plot_ly(d, x = carat, y = price, z=depth, text = paste("Clarity: ", clarity),
        type="scatter3d", mode="markers")

p %>% 
add_trace(x = 2.5, y = 5000, z = 0, type = "scatter3d", text = "test", mode = "text") %>% 
add_trace(x = 2.5, y = 15000, z = 0, type = "scatter3d", text = "test", mode = "text") %>% 
layout(showlegend = FALSE)

enter image description here


嗨@MLavoie,我无法使其工作,并在原帖中包含了我的尝试,请您再次查看。谢谢! - MVWyck
当我运行您的代码时,我没有任何问题,但是我有一个不同的 plotly 版本(plotly_3.4.13)。请尝试升级您的版本并重新运行代码。 - MLavoie
搞定了!非常感谢!!!我不得不升级plotly和ggplot2,但现在它可以工作了。 - MVWyck
抱歉!我以为我已经做了,但现在刚刚完成了!谢谢,祝你周末愉快! - MVWyck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接