【发布时间】:2020-06-25 02:08:30
【问题描述】:
此代码制作了 Fisher iris 数据集的简单 3d 散点图,并添加了一个额外的分类变量:
library(plotly)
roots <- factor(round(runif(n = dim(iris)[2],min = -.499,max = 2.499)))
my_iris <- cbind(data.frame(roots), iris)
plot_ly() %>%
add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
x = ~Sepal.Length,
y = ~Petal.Length,
z = ~Sepal.Width,
color = ~Species,
colors = c("red","blue","green")
)
通过查看此帮助页面:https://plot.ly/r/marker-style/
我发现您可以像这样为这些点添加轮廓:
plot_ly() %>%
add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
x = ~Sepal.Length,
y = ~Petal.Length,
z = ~Sepal.Width,
color = ~Species,
colors = c("#00FA9A34","#B22222dd","#00BFFFee"),
marker = list(
line = list(
color = "#aabbffdd",
width = 2
)
)
)
查看此站点https://plot.ly/r/reference/#scatter3d 使我们认为线条是 scatter3d 标记的属性,而这些标记又具有颜色和宽度属性。
现在我尝试根据我的新 roots 变量将颜色映射到轮廓,
plot_ly() %>%
add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
x = ~Sepal.Length,
y = ~Petal.Length,
z = ~Sepal.Width,
color = ~Species,
colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
marker = list(
line = list(
color = ~roots,
colors = c("#000000ff","#f00f3355","#dd22ccbb"),
width = 2
)
)
)
而且它不太有效:我使用的第一个 hex+alpha 值应该映射到完全不透明的黑色,但这不是我得到的颜色之一,我希望看到描述输出的图例条目。
所以我的问题是:有没有办法进行这种美学映射?也许我应该使用add_markers而不是使用add_trace?有没有办法在 Plotly R 的二维散点图中做到这一点?还希望获得有关如何正确学习 R 的 Plotly 的提示,因为我在上面链接到的文档页面有点不透明,而且似乎比 ggplot2 学习的好资源更少。
【问题讨论】:
标签: r plot plotly data-visualization r-plotly