我如何明确地为R散点图中的每个点分配唯一的颜色?

问题描述 投票:0回答:1

我有一些这样的数据:

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')))

图:

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

使用Plotly在R中为这样的点着色的正确方法是什么?另外,通常如何用Plotly为R中的数据点分配颜色?

[为澄清起见,我正在尝试为颜色为“ #XXYYZZ”格式的每个点上色,其中“ XX”是介于00和FF之间的值,线性映射到data $ x的值,范围是从0到1。 X维度确定红色的数量,Y维度确定绿色的数量,Z维度确定蓝色的数量。在0,0,0处的点应为黑色,在1,1,1处的点应为白色。

r plotly scatter3d colorbrewer
1个回答
0
投票

[在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')))

图:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.