Andrea
Andrea

Reputation: 77

Change colors for NA values to gray in R plotly scatter 3d

I'm trying to generate a plot in which I want to show NA values in translucent gray, and a set of numeric values I want to plot with a blue-red color scale. By default, plotly colors my NAs with a dark gray solid color, but I figure there must be a way to change that somehow. Is there anyway to do it? Here is a mockup of my code:

library("plotly")

blue_red <- rev(c("#A50026", "#D73027", "#F46443", "#FDAE61", "#FEE090"
                , "#E0F3F8", "#ABD9E9", "#74ADD1", "#4575B4", "#313695"))

mtcars$brand <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
mtcars[mtcars$brand=="Merc","mpg"] <- NA

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, 
        marker = list(size = 6),
        color=~mpg,
        colors = blue_red,
        type = "scatter3d", 
        mode = "markers")
fig

enter image description here

I found a somewhat similar question here: https://community.plotly.com/t/how-to-change-colors-for-na-values-to-gray-in-a-choropleth-map/15746/3 but I haven't been able to translate that to solve my problem.

Upvotes: 0

Views: 284

Answers (1)

Carl Witthoft
Carl Witthoft

Reputation: 21492

Edited to make sense OK, so you have known triplets for x,y,z coordinates and you want to assign colors to various values of mpg. Can you simply do the following?

mtcars[mtcars$brand=="Merc","mpg"] <- your_chosen_value and assign a desired color to that value.

If that distorts the colors mapped to numeric values that are genuine, maybe it's simpler to plot all the non-"Merc" data with one call and then plot all the "Merc" data with a second call that uses a simple, single-value color assignment instead of your blue_red vector.

Upvotes: 0

Related Questions