Search code examples
rplotlyscatter-plot

Plotly: different symbols on scatterplots


library(dplyr)
library(plotly)
library(plyr)

df <- data.frame(
    Category = c('foo', 'bar', 'bar', 'foo'),
    x = c(2.1, 3.4, 4, 4),
    y = c(16, 21, 10, 17)
)

palette <- c("#007700", "#cc0000")
fig <- plot_ly(data = df, x = ~x, y = ~y, size = 10, color = ~Category,
               colors = palette)
fig

How would I get the foo points to plot as squares and the bar points as triangles?


Solution

  • Add symbol=~Category and define symbols=:

    plotly::plot_ly(
      data = df, x = ~x, y = ~y, size = 10, color = ~Category, colors = palette,
      symbol = ~Category, symbols = c(15, 17)
    )
    

    plotly with squares and triangles