плотно добавляем colorbar в mesh3d без использования интенсивности (используя facecolor) - PullRequest
0 голосов
/ 13 ноября 2018

Мне нужно поместить цветную полосу на график mesh3d, который состоит из нескольких трасс. Каждая трассировка mesh3d имеет один цвет, но мне нужна цветовая панель, чтобы охватить все цвета трассировки.

Я пытаюсь объединить scatter3d с visible="legendonly" с mesh3d, так что добейтесь этого. Но когда строится сетка, легенда удаляется.

Используя вертолет-пример:

library(plotly)
library(geomorph)

plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
dest <- basename(plyFile)
if (!file.exists(dest)) {
  download.file(plyFile, dest)
}

mesh <- read.ply(dest, ShowSpecimen = F)
# see getS3method("shade3d", "mesh3d") for details on how to plot

# plot point cloud
x <- mesh$vb["xpts",]
y <- mesh$vb["ypts",]
z <- mesh$vb["zpts",]
m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

# now figure out the colormap
zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

library(scales)
facecolor = colour_ramp(
  brewer_pal(palette="RdBu")(9)
)(rescale(x=zmean))


plot_ly()  %>%
  # Creates the legend, and also the plotting space
  add_trace(
    x = x, y = y, z = z,
    color = x,
    colors = c("#ffffff", "#000000"),
   # visible="legendonly",
    type = "scatter3d",
    mode="markers"
  ) %>% 

  # Adds the mesh, but removes the legend
  add_trace(
    x = x, y = y, z = z,
    i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
    facecolor = facecolor,
    type = "mesh3d"
  )

1 Ответ

0 голосов
/ 14 ноября 2018

После долгих взломов у меня наконец-то есть рабочее решение.В этом случае, путем построения области mesh3d с точками, которые находятся «внутри» вертолета и не будут видны позже, а затем нанесите фактический вертолет позже.

Кажется, что «visible = 'legendonly» »не применяется к mesh3d, так как этот параметр удаляет как сюжет, так и легенду.

library(plotly)
library(geomorph)

plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
dest <- basename(plyFile)
if (!file.exists(dest)) {
  download.file(plyFile, dest)
}

mesh <- read.ply(dest, ShowSpecimen = F)
# see getS3method("shade3d", "mesh3d") for details on how to plot

# plot point cloud
x <- mesh$vb["xpts",]
y <- mesh$vb["ypts",]
z <- mesh$vb["zpts",]
m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))

# now figure out the colormap
zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

# Get colors you want
cols = brewer_pal(palette="RdBu")(9)

# Ramp to add to facecolor
library(scales)
facecolor = colour_ramp(cols)(rescale(x=zmean))

# Create data.frame of colours and breakpoints.
# Must go from 0 to 1, plotly scales it based on values it self.
colz = data.frame(seq(0,1,length.out = length(cols)), 
              cols)

# Make stupid pointcloud to fool the colorbar
xx = c(min(x), max(x))
yy = c(min(y), max(y))
zz = c(min(z), max(z))

plot_ly()  %>%

  # Creates the legend, and also the plotting space
  add_trace(
    x = xx, y = yy, z = zz,
    intensity = x,
    colorscale = colz,
   # visible="legendonly",
    type = "mesh3d"
  ) %>% 


  # Adds the mesh
  add_trace(
    x = x, y = y, z = z,
    i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
    facecolor = facecolor,
    showscale=FALSE,
    type = "mesh3d"
  )
...