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?
I’m new to the forum and to using the plotly library, so I may be overlooking a super simple solution…
Thanks!
You can use the symbol
argument to set the mapping:
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)
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
)
)
)