dantarno
dantarno

Reputation: 92

How can I add scatter point to an existing 3D scatter plot in plotly

I created a 3D scatter plot using plotly in R but I have trouble adding more data to the 3D plot.

library(plotly)
names(b) <- c('m1','m2','m3','clust')

umap_1 <- b$m1
umap_2 <- b$m2
umap_3 <- b$m3
clust <- b$clust

palette <- c('#e86e41','#d9b53f','#9857ba','#57ba64','#57b7ba')

p <- plot_ly(x = umap_1, y = umap_2, z =umap_3, 
        color = b$clust,
        colors = palette,
        marker = list(size = 1, width=1),
        text = b$clust,
        hoveringinfo = 'text'# controls size of points
        ) 

I would like to add to this scatter plot other points with other size and color.

I thought first about adding a fifth column to my dataset and using this in marker with something like ifelse(b$V4=='something'], size=1, size=5) but it doesn't work.

So i thought about adding element using %>% like :

p <- plot_ly(x = umap_1, y = umap_2, z =umap_3, 
        color = b$clust,
        colors = palette,
        marker = list(size = 1, width=1),
        text = b$clust,
        hoveringinfo = 'text'
        ) 
p <- p >%> add_something(x,y,z, marker = list(size = 5, width=1))

But there is no function to add scatter point to a 3D plot.

Is there a way to do so ?

Upvotes: 1

Views: 3529

Answers (2)

Benjamin Ziepert
Benjamin Ziepert

Reputation: 1754

You can add several traces as in the example below.

fig <- plot_ly(data = df, x = ~x, y = ~y, z = ~z) %>%

  add_trace(
    data = df2
    , x = ~x
    , y = ~y
    , z = ~z
    , color= ~group
    , mode = "markers"
    , type = "scatter3d"
    , marker = list(size = 5))

See for a related example: Mixing surface and scatterplot in a single 3D plot

Upvotes: 1

Josiah Coad
Josiah Coad

Reputation: 355

In python, you can do:

fig = px.line_3d(...)
fig.add_scatter3d(...)

Upvotes: 1

Related Questions