简体   繁体   English

plotly 3d 散点 plot 带实心透明 colors in R

[英]plotly 3d scatter plot with solid and transparent colors in R

I'm trying to create a 3d scatter plot with plotly in R. I want to color the data points by a categorical value, using a custom color scale.我正在尝试在 R 中创建一个 3d 散点图 plot 和 plotly。我想使用自定义色标按分类值为数据点着色。 I need some of the values to be solid colors and others to be transparent.我需要一些值是固定的 colors 而其他值是透明的。 Here is a mockup of my code:这是我的代码的模型:

mtcars$brand <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
mtcars$brand <- as.factor(mtcars$brand)

custom_colors <- c("#ebac23", "#b80058", "#008cf9", "#006e00", "#00bbad"
                 , "#d163e6", "#b24502", "#ff9287", "#5954d6", "#00c6f8"
                 , "#878500", "#00a76c", "#bdbdbd", "#846b54", "#5F2F87"
                 , "#241346", "#B3EE3A", "#077187", "#FFE06A", "#73787E"
                 , "#A6CEE3", "#1F78B4")
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~brand
             , colors = custom_colors, type = "scatter3d", mode = "markers")
fig <- fig %>% add_markers()

fig

What I would need is ie: to color "Cadillac" and "Ferrari" with a transparent color, and all the other brands with solid colors. I've tried using two traces, one with the transparent colors and one with the solid ones, and transforming all my colors into rgba.我需要的是:用透明颜色给“凯迪拉克”和“法拉利”上色,用纯色给所有其他品牌上色 colors。我试过使用两条轨迹,一条是透明的 colors,一条是实心的,并将我所有的 colors 转换为 rgba。 However that does not work as intended, since it just colors the points by trace and not by each individual brand.然而,这并没有按预期工作,因为它只是 colors 跟踪点,而不是每个单独的品牌。 The only solution I can think of is adding each brand as a trace, but there has to be a simpler way of doing it, right...我能想到的唯一解决方案是将每个品牌添加为跟踪,但必须有更简单的方法,对吧......

RIGHT???正确的???

The following code allows you to select the transparancy level for the factors of your choice.以下代码允许您将 select 的透明度级别作为您选择的因素。

I directly assigned the color and opacity level to each car model.我直接给每辆车 model 分配了颜色和不透明度级别。

### Import libraries
library(plotly)
library(dplyr)

### Import data
data(mtcars)
mtcars$brand <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
mtcars$brand <- as.factor(mtcars$brand)
mtcars <- mtcars[order(mtcars$brand), ]

# Set opacity
mtcars$custom_opacity <- ifelse(mtcars$brand %in% c("Lincoln", "Cadillac"), 0.5, 1)

# Set colors
mtcars$custom_colors <- c("#ebac23", "#b80058", "#008cf9", 
                          "#006e00", "#00bbad", "#d163e6", 
                          "#b24502", "#ff9287", "#5954d6", 
                          "#00c6f8", "#878500", "#00a76c", 
                          "#bdbdbd", "#846b54", "#5F2F87", 
                          "#241346", "#B3EE3A", "#077187", 
                          "#FFE06A", "#73787E", "#A6CEE3", 
                          "#1F78B4", "#B3EE3A", "#077187", 
                          "#FFE06A", "#73787E", "#A6CEE3", 
                          "#1F78B4", "#FFE06A", "#73787E", 
                          "#A6CEE3", "#1F78B4")

#### Display plot
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, 
               color = ~mtcars$brand, 
               colors = mtcars$custom_colors, 
               opacity = mtcars$custom_opacity, 
               type = "scatter3d", 
               mode = "markers")

fig <- fig %>% add_markers()

fig

在此处输入图像描述

As you can see, our two labels Lincoln and Cadillac are transparant (though it was Ferrari in your question).如您所见,我们的两个标签LincolnCadillac是透明的(尽管在您的问题中是 Ferrari)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM