Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

How can I explicitly assign unique colors to every point in an R Plotly scatterplot?

$
0
0

I have some data like this:

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

I want a scatterplot with points colored independently/discretely in each dimension (X, Y, and Z) using RGB values.

This is what I have tried:

Code:

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

Plot:

enter image description here

RColorBrewer thinks I'm trying to create a continuous scale from 500 intermediate colors:

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

What are some correct ways to color the points like this in R with Plotly? Also, how can one generally assign colors to data points in R with Plotly, individually?

To clarify, I am trying to color each point where the color is of the format "#XXYYZZ" where 'XX' a value between 00 and FF linearly mapped to the value of data$x from 0 to 1. That is, the X dimension determines the amount of red, the Y dimension determines the amount of green, and the Z dimension determines the amount of blue. At 0,0,0 the point should be black and at 1,1,1 the point should be white. The reason for this is to make as easy to visualize the 3D position of the points as possible.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>