如何在scatter3d中使绘图标记标签与标记具有相同的颜色?

时间:2019-07-23 19:42:07

标签: r plotly r-plotly

我的3d散点图上的标签颜色与标记颜色不匹配: enter image description here

所有标签最终都使用相同的颜色,并带有以下最小示例:

plot_ly(
     data      = mtcars,
     type      = "scatter3d",
     mode      = "markers",
     colors    = c("blue","red"),
     text      = row.names(mtcars),
     hoverinfo = "text",
     color     = ~cyl,
     x         = ~disp,
     y         = ~hp,
     z         = ~qsec
)

在2D散点图中,标签会正确着色: enter image description here enter image description here

可以通过以下方式实现:

plot_ly(
     data      = mtcars,
     type      = "scatter",
     mode      = "markers",
     colors    = c("blue","red"),
     text      = row.names(mtcars),
     hoverinfo = "text",
     color     = ~cyl,
     x         = ~disp,
     y         = ~hp
)

在这个示例中,我知道图例的颜色范围不合适,但是对于本代码使用的实际数据来说,它是必需的。

1 个答案:

答案 0 :(得分:0)

好吧,如果您设置color = ~as.factor(cyl)似乎可以正常工作。

但是我真的不知道为什么。

代码:

plot_ly(
  data      = mtcars,
  type      = "scatter3d",
  mode      = "markers",
  colors    = ~c("blue","red"),
  text      = row.names(mtcars),
  hoverinfo = "text",
  color     = ~as.factor(cyl),
  x         = ~disp,
  y         = ~hp,
  z         = ~qsec
)

输出:

enter image description here