r - 如何为 R Plotly 散点图中的每个点显式分配唯一的颜色?

标签 r plotly scatter3d colorbrewer

我有一些这样的数据:

data <- data.frame(x=runif(500), y=runif(500), z=runif(500))

我想要一个散点图,其中的点使用 RGB 值在每个维度(X、Y 和 Z)中独立/离散地着色。

这是我尝试过的:

代码:

library(dplyr)
library(plotly)

xyz_colors <- rgb(data$x, data$y, data$z)

plot_ly(data = data, 
    x = ~x, y = ~y, z = ~z, 
    color= xyz_colors, 
    type = 'scatter3d', 
    mode='markers') %>%
  layout(scene = list(xaxis = list(title = 'X'),
                      yaxis = list(title = 'Y'),
                      zaxis = list(title = 'Z')))

plotly :

enter image description here

RColorBrewer 认为我正在尝试从 500 种中间颜色创建连续的比例:

Warning messages:
1: In RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

2: In RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

在 R 中使用 Plotly 对点进行着色的正确方法有哪些? 另外,如何使用 Plotly 为 R 中的数据点单独分配颜色?

为了澄清,我试图对颜色格式为“#XXYYZZ”的每个点进行着色,其中“XX”是 00 和 FF 之间的值,线性映射到 data$x 从 0 到 1 的值。即,X 维度决定红色量,Y 维度决定绿色量,Z 维度决定蓝色量。在 0,0,0 处该点应为黑色,在 1,1,1 处该点应为白色。这样做的原因是为了尽可能轻松地可视化点的 3D 位置。

最佳答案

评论后更新答案:

So, is there no way to color every point separately?

是的,通过 add_traces() 的强大功能和灵活性可以实现。而且它比我最初想象的要简单得多。

只需设置一个具有一些所需 3D 功能的空绘图图形:

p <-plot_ly(data = data, type = 'scatter3d', mode='markers')

并申请add_traces()在每个定义的颜色上循环:

for (i in seq_along(xyz_colors)){
  p <- p %>% add_trace(x=data$x[i], y=data$y[i], z=data$z[i],
                       marker = list(color = xyz_colors[i], opacity=0.6, size = 5),
                       name = xyz_colors[i])
}

您可以使用您选择的颜色轻松定义单个点,如下所示:

p <- p %>% add_trace(x=0, y=0, z=0,
                       marker = list(color = rgb(0, 0, 0), opacity=0.8, size = 20),
                       name = xyz_colors[i])

plotly :

enter image description here

完整代码:

library(dplyr)
library(plotly)

# data and colors
data <- data.frame(x=runif(500), y=runif(500), z=runif(500))
xyz_colors <- rgb(data$x, data$y, data$z)

# empty 3D plot
p <-plot_ly(data = data, type = 'scatter3d', mode='markers') %>%
   layout(scene = list(xaxis = list(title = 'X'),
                      yaxis = list(title = 'Y'),
                      zaxis = list(title = 'Z')))

# one trace per color
for (i in seq_along(xyz_colors)){
  p <- p %>% add_trace(x=data$x[i], y=data$y[i], z=data$z[i],
                       marker = list(color = xyz_colors[i], opacity=0.6, size = 5),
                       name = xyz_colors[i])
}

# Your favorite data point with your favorite color
p <- p %>% add_trace(x=0, y=0, z=0,
                       marker = list(color = rgb(0, 0, 0), opacity=0.8, size = 20),
                       name = xyz_colors[i])

p

原始答案:

在 3D 图中,您可以对所有点使用相同的颜色,使用不同的颜色区分不同的簇或类别,或者对每个点使用单独的颜色来说明第四个值(或第四个维度,如果您愿意) ,如数据集中所述 here )。正如您所说,所有这些方法都是 '[...] correct ways to color the points [...]' 的示例。 。看看下面,看看这是否适合您的需求。我已经包括了fourthVal <- data$x+data$y+data$z作为额外维度的示例。您最终使用的内容完全取决于您的数据集以及您想要说明的内容。

代码:

library(dplyr)
library(plotly)

data <- data.frame(x=runif(500), y=runif(500), z=runif(500))

xyz_colors <- rgb(data$x, data$y, data$z)

fourthVal <- data$x+data$y+data$z

plot_ly(data = data, 
        x = ~x, y = ~y, z = ~z, 
        color= fourthVal, 
        type = 'scatter3d', 
        mode='markers') %>%
  layout(scene = list(xaxis = list(title = 'X'),
                      yaxis = list(title = 'Y'),
                      zaxis = list(title = 'Z')))

plotly :

enter image description here

关于r - 如何为 R Plotly 散点图中的每个点显式分配唯一的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58615373/

相关文章:

c++ - 将数据从 C++ 加载到 QML Scatter3d 项

r - 如何使用 png 设备为 heatmap.2 绘图添加更多边距?

r - 在R函数中基于变量名称将位置参数转换为命名参数

r - 是否有更优雅的方法将参差不齐的数据转换为整洁的数据框

plotly - Plotly 的默认色标是什么?

python - 使用plotly.express 的固定比例轴facetrow/facet_col

r - 如何防止 knitr 打印 ## 和矩阵索引/行号?

r - 文件错误(con, "rb"): cannot open the connection

matplotlib - 在 matplotlib scatter3d 中设置 zlim