R Plotly 3D:在不继承配色方案的情况下将网格添加到散点图
R Plotly 3D: Add Mesh to Scatter plot without inheriting color scheme
基本上我有一个散点 3d 散点图,其中的点分为两类(选中的、未选中的),分别用红色和灰色表示。为了更好地可视化所选体积,我想添加一个蓝色的低不透明度立方体。但是,当我为立方体添加网格时,立方体显示为绿色,未选中的点显示为橙色而不是灰色。
简而言之:为什么立方体不是蓝色的,未选中的点不是灰色的,我怎样才能让它们变成灰色?
library(shiny)
library(plotly)
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5),
MESH_COL = factor(rep("CUBE", 8), levels = c("CUBE")))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
#Here the trouble starts
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
color = ~MESH_COL,
colors = c("blue"),
inherit = FALSE,
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
非常感谢任何帮助。
尝试 facecolor
:
library(shiny)
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
mycolors <- colours()[2:10]
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
facecolor = rep("blue", 12),
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
由 reprex package (v0.3.0)
于 2020 年 7 月 2 日创建
基本上我有一个散点 3d 散点图,其中的点分为两类(选中的、未选中的),分别用红色和灰色表示。为了更好地可视化所选体积,我想添加一个蓝色的低不透明度立方体。但是,当我为立方体添加网格时,立方体显示为绿色,未选中的点显示为橙色而不是灰色。
简而言之:为什么立方体不是蓝色的,未选中的点不是灰色的,我怎样才能让它们变成灰色?
library(shiny)
library(plotly)
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5),
MESH_COL = factor(rep("CUBE", 8), levels = c("CUBE")))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
#Here the trouble starts
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
color = ~MESH_COL,
colors = c("blue"),
inherit = FALSE,
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
非常感谢任何帮助。
尝试 facecolor
:
library(shiny)
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
mycolors <- colours()[2:10]
ui <- fluidPage(
tags$h2("This is my 3D plot."),
plotlyOutput("Plot3d", width = "1000px", height = "1000px")
)
server <- function(input, output, session){
output$Plot3d <- renderPlotly ({
#Defining data frame for scatter
df_scatter <- data.frame(X_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Y_VAL = rnorm(50, mean = 0.5, sd = 0.15),
Z_VAL = rnorm(50, mean = 0.5, sd = 0.15),
SCATTER_COL = rep("unselected", 50))
#Every point inside of the cube is labeled "selected"
for (i in 1:nrow(df_scatter)){
if (df_scatter$X_VAL[i] < 0.5 && df_scatter$Y_VAL[i] < 0.5 && df_scatter$Z_VAL[i]< 0.5) {
df_scatter$SCATTER_COL[i] <- "selected"
}
}
df_scatter$SCATTER_COL <- factor(df_scatter$SCATTER_COL, levels = c("selected", "unselected"))
#Defining data frame for mesh
df_mesh <- data.frame(X_VAL = c(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5),
Y_VAL = c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
Z_VAL = c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5))
plot_ly()%>%
add_markers(type = "scatter3d",
mode = "markers",
data = df_scatter,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
color = ~SCATTER_COL,
colors = c('red', 'grey')) %>%
add_trace(type = 'mesh3d',
data = df_mesh,
x = ~X_VAL,
y = ~Y_VAL,
z = ~Z_VAL,
i = c(7, 0, 0, 0, 4, 4, 6, 1, 4, 0, 3, 6),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 6, 5, 5, 7, 2),
facecolor = rep("blue", 12),
opacity = 0.1
)
})
}
shinyApp(ui = ui, server=server)
由 reprex package (v0.3.0)
于 2020 年 7 月 2 日创建