在 Plotly 中为 3D 散点图添加注释
Add Annotation to 3D Scatterplot in Plotly
我是这个领域的新手,希望得到一些帮助。我刚刚开始使用 Plotly 并使用它来构建一个非常基本的 3D 散点图,该散点图可以模拟一些货架以及哪些物品在哪里。当我构建 "shelf" 并呈现绘图时,我想在文本中添加一个注释,指示我正在查看哪一组货架。
Here is the current output of my program 和
this is what I am trying to do
我已经尝试查看文档并尝试添加注释(就像我对 2D 模拟所做的那样),但在代码编译时,文本没有显示。
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)
提前致谢!麦克
#
更新
感谢@MLavoie 的回答,很抱歉排除了可重现的数据集。不幸的是我无法复制你所做的。我注意到你的图上的 z 轴设置为零,我试图重置我的但没有成功。但是,即使将点调整到绘图中运行ge,我仍然无法显示它。这是我得到的情节:
当我 运行 这个代码
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
再次感谢!
使用 iris 数据集(您没有提供可重现的示例)您可以尝试:
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)
我是这个领域的新手,希望得到一些帮助。我刚刚开始使用 Plotly 并使用它来构建一个非常基本的 3D 散点图,该散点图可以模拟一些货架以及哪些物品在哪里。当我构建 "shelf" 并呈现绘图时,我想在文本中添加一个注释,指示我正在查看哪一组货架。
Here is the current output of my program 和
this is what I am trying to do
我已经尝试查看文档并尝试添加注释(就像我对 2D 模拟所做的那样),但在代码编译时,文本没有显示。
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)
提前致谢!麦克
#更新
感谢@MLavoie 的回答,很抱歉排除了可重现的数据集。不幸的是我无法复制你所做的。我注意到你的图上的 z 轴设置为零,我试图重置我的但没有成功。但是,即使将点调整到绘图中运行ge,我仍然无法显示它。这是我得到的情节:
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
再次感谢!
使用 iris 数据集(您没有提供可重现的示例)您可以尝试:
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)