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)
8
  • it should be downloadable through the onedrive link in the question Maximillian
    – Mark
    Nov 24, 2017 at 14:42
  • Thanks! Didn't see the link :/ Nov 24, 2017 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. plot.ly/r/reference Nov 24, 2017 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, 2017 at 23:46
  • I tried passing a list of colors with different alpha values to markers but that didn't work for me. Nov 24, 2017 at 23:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.