fork download
  1. # your code goes herelibrary(plotly)
  2.  
  3. # Sample data
  4. x <- 1:10
  5. y1 <- x^2
  6. y2 <- x^3
  7. linetype <- c(rep("solid", 5), rep("dash", 5))
  8. color <- c(rep("blue", 5), rep("red", 5))
  9.  
  10. # Create the plot
  11. p <- plot_ly(
  12. data = data.frame(x, y1, y2, linetype, color),
  13. type = "scatter",
  14. mode = "lines"
  15. ) %>%
  16. # First trace with color and linetype mapping
  17. add_trace(
  18. x = ~x,
  19. y = ~y1,
  20. line = list(dash = ~linetype),
  21. legendgroup = ~linetype, # Group by linetype
  22. linetype = ~linetype,
  23. color = I("black"), # Set a static color so color doesn't create a new legend
  24. showlegend = TRUE # Only show legend for linetype
  25. ) %>%
  26. # Second trace for y2 with color and linetype mapping
  27. add_trace(
  28. x = ~x,
  29. y = ~y2,
  30. line = list(dash = ~linetype),
  31. legendgroup = ~linetype,
  32. linetype = ~linetype,
  33. color = I("black"), # Same static color to avoid color legend entries
  34. showlegend = FALSE # Hide color legend
  35. )
  36.  
  37. # Display the plot
  38. p
Success #stdin #stdout #stderr 0.23s 41044KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: could not find function "%>%"
Execution halted