# Change plot\_ly marker shape by variable

**URL:** <https://community.plotly.com/t/change-plot-ly-marker-shape-by-variable/58032>\
**Category:** Plotly R\
**Created:** [October 30, 2021, 7:16pm UTC](https://community.plotly.com/t/change-plot-ly-marker-shape-by-variable/58032 "2021-10-30T19:16:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IzaPizza](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@IzaPizza](https://community.plotly.com/u/IzaPizza)\
**Post date:** [October 30, 2021, 7:16pm UTC](https://community.plotly.com/t/change-plot-ly-marker-shape-by-variable/58032/1 "2021-10-30T19:16:43Z")

</div>

Hi,

I am trying to create a bubble plot, that plots _positive_ and _relaxed_, changes marker colour based on _family_ (4 factors), and changes marker size based on _anger_. I get this to work like so:

fig ← plot\_ly(ag\_data, x = ~positive, y = ~relaxed, color = ~family, size = ~anger,  
type = ‘scatter’, mode = ‘markers’, fill=~’’, marker = list(sizemode = ‘area’))  
fig

However, I would also like to change the marker **shape** based on _family_. A circle, triangle, square, and diamond shape for example, just so it is easier to see for anyone who can’t distinguish the colours. I can’t seem to find a way to do this! Is it possible at all or should I be using a different function?

 ![Plotly_Test](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/3/2/32d68960bc1d0711ad1f9aa60b991bcbc550bc93.jpeg)

I’m new to the forum and to using the plotly library, so I may be overlooking a super simple solution…

Thanks!

---

<div class="post-metadata">

**Author:** ![ismirsehregal](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/ismirsehregal/32/6794_2.png) [@ismirsehregal](https://community.plotly.com/u/ismirsehregal)\
**Post date:** [November 1, 2021, 2:25pm UTC](https://community.plotly.com/t/change-plot-ly-marker-shape-by-variable/58032/2 "2021-11-01T14:25:43Z")

</div>

You can use the `symbol` argument to set the mapping:

```auto
library(plotly)

DF <- data.frame(x = 1:40, y = 1:40, id = rep(LETTERS[1:4], each = 10))

plot_ly(DF, x = ~x, y = ~y, symbol = ~id, type = "scatter", mode = "markers")

# specify symbols: 
# plot_ly(DF, x = ~x, y = ~y, symbol = ~id, symbols = as.character(seq_along(unique(DF$id))), type = "scatter", mode = "markers")

```

Also see `schema()` and navigate  
object ► traces ► scatter ► attributes ► marker ► symbol ► values

The following displays all symbols currently available (modified version of [this code](https://plotly-r.com/working-with-symbols.html))

```auto
vals <- schema(F)$traces$scatter$attributes$marker$symbol$values
vals <- grep("[a-z]", vals, value = TRUE)

grid_size <- ceiling(sqrt(length(vals)))

plot_ly() %>%
  add_markers(
    x = rep(seq_len(grid_size), each = grid_size, length.out = length(vals)),
    y = rep(seq_len(grid_size), times = grid_size, length.out = length(vals)),
    text = vals,
    hoverinfo = "text",
    marker = list(
      symbol = vals,
      size = 30,
      line = list(
        color = "black",
        width = 2
      )
    )
  )

```

![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/6/5/6527e0ccdf2d8424716e2818e61d66a175c6e4eb.png)
