-1

In ggplot(), I can dynamically change the lwd variable within aes. However, if I use plot_ly() instead, this option is not available.

I am also aware of ggplotly to convert a ggplot to plotly, but there are issues using geom_line() in ggplotly, as seen in this question: ggplotly not displaying geom_line correctly

I am currently using plotly in R, but can convert my codebase to python, if necessary.

Community
  • 1
  • 1
Adam_G
  • 7,337
  • 20
  • 86
  • 148
  • Why the downvote/vote to close? – Adam_G Apr 22 '16 at 14:56
  • I know you can do that for markers by setting `marker = list(size = ...)` but I don't think its possible for lines. Atleast not like `aes` in `ggplot()`. You could hack your way around it by adding each group as a separate trace when using `plot_ly()` – royr2 Apr 25 '16 at 10:40
  • Thanks. What do you mean by that? – Adam_G Apr 25 '16 at 14:45

1 Answers1

1

@Adam_G see below. It is hack-y of sorts so there might be a better way to do this. Someone more knowledgeable than me might be able to shed some light on this.

library(plotly)
library(dplyr)

ds <- data.frame(x = 1:100, 
                 y = sample(1:1000, size = 100), 
                 group = sample(LETTERS[1:3], size = 100, replace = T))

# Default
plot_ly(ds, x = x, y = y, group = group, mode = "lines")

# Manually change linewidth (if not too many groups)
plot_ly(ds %>% filter(group == "A"), x = x, y = y, mode = "lines", line = list(width = 2)) %>% 
  add_trace(data = ds %>% filter(group == "B"), x = x, y = y, mode = "lines", line = list(width = 5)) %>% 
  add_trace(data = ds %>% filter(group == "C"), x = x, y = y, mode = "lines", line = list(width = 10))

# In a loop (if too many groups)
p <- plot_ly()
vec <- unique(ds$group)
widths <- c(2, 5, 10)

for(i in 1:3){
  # Note that evaluate  = T is important here

  p <- add_trace(p, data = ds %>% filter(group == vec[i]), x = x, y = y, mode = "lines",
                 line = list(width = widths[i]), evaluate = T)
}

p
royr2
  • 2,239
  • 15
  • 20