4

How to make a scatter3d look like a density plot in 3d?

I am trying to set the opacity (or some other variable) in a plot_ly scatter3d plot to use a value per point in the plot, with the values coming from a column in the dataframe. This in order to create a sort of 3d scatter density plot. In this case, the opacity column contains random values, but if this turns out to be possible, I want to make points LESS opaque when the amount of nearby points is high, and opaque when they are relatively isolated. Another approach to make this look like a density plot while perserving the scatter3d style is fine as well.

My attempts to play with opacity or alpha in the plot_ly code has failed and I could not find a working example.

The dataset can be downloaded from here: file

library(shiny)
library(plotly)
library(data.table)


mydf <- read.csv("myshowdf.csv")


ui <- fluidPage(
  plotlyOutput('scatter', width = 800, height = 600)  
  )

server <- function(input, output, session) {

  output$scatter <- renderPlotly({
    ppl <- plot_ly(mydf, x = ~FL.Red.Range, y = ~FL.Yellow.Range, z = ~SWS.Length, mode = 'markers',
                   color = ~sub.flow.FP1,   marker = list(size = 4, opacity =mydf$opacity),
                   colors = c('blue', 'green', 'red', 'yellow'),
                   type = 'scatter3d', source = 'scatter') %>%
      layout( autosize = F,
              margin = list(l = 40, r = 20, b = 20, t = 40, pad = 4)
      )
    ppl$elementId <- NULL
    ppl
  })
  }

shinyApp(ui = ui, server = server)
Mark
  • 2,789
  • 1
  • 26
  • 66
  • it should be downloadable through the onedrive link in the question Maximillian – Mark Nov 24 '17 at 14:42
  • Thanks! Didn't see the link :/ – Maximilian Peters Nov 24 '17 at 15:40
  • I think you cannot do that. From the documentation: opacity (number between or equal to 0 and 1) Sets the marker opacity. Note that the marker opacity for scatter3d traces must be a scalar value for performance reasons. To set a blending opacity value (i.e. which is not transparent), set "marker.color" to an rgba color and use its alpha channel. https://plot.ly/r/reference/ – Maximilian Peters Nov 24 '17 at 23:02
  • Possibly, I wasnt sure. Basically what I was looking for was to set a unique opacity or alpha value for each individual point – Mark Nov 24 '17 at 23:46
  • I tried passing a list of colors with different alpha values to markers but that didn't work for me. – Maximilian Peters Nov 24 '17 at 23:48
  • Same conclusion as my trials then. As we say in Italy "va bo", or so be it. Thanks for trying maximiallian! – Mark Nov 24 '17 at 23:49
  • Let's continue in chat: https://chat.stackoverflow.com/rooms/info/159787/plotly?tab=general – Maximilian Peters Nov 24 '17 at 23:50
  • Ok maximillian, hope to chat to you soon there then. – Mark Nov 26 '17 at 16:32

0 Answers0