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. y3 <- x^3 + 20
  8. linetype <- c(rep("solid", 5), rep("dash", 5), rep("dot",5))
  9. color <- c(rep("blue", 5), rep("red", 5), rep("green",5))
  10.  
  11. # Create the plot
  12. p <- plot_ly(
  13. data = data.frame(x, y1, y2, y3, linetype, color),
  14. type = "scatter",
  15. mode = "lines",
  16. x = ~x,
  17. y = ~y1,
  18. color = ~color, # Keep color mapping, but will not show on legend
  19. linetype = ~linetype,
  20. legendgroup = ~linetype, # Group by linetype for legend entries
  21. showlegend = TRUE # Show legend for the first entry of each group (linetype)
  22. ) %>%
  23. add_trace(
  24. y = ~y2,
  25. linetype = ~linetype,
  26. color = ~color,
  27. legendgroup = ~linetype,
  28. showlegend = FALSE # Don't trigger a legend entry for subsequent lines with the same linetype
  29. ) %>%
  30. add_trace(
  31. y = ~y3,
  32. linetype = ~linetype,
  33. color = ~color,
  34. legendgroup = ~linetype,
  35. showlegend = FALSE # Don't trigger a legend entry for subsequent lines with the same linetype
  36. )
  37.  
  38. # display the plot
  39. p
Success #stdin #stdout #stderr 0.32s 40820KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: could not find function "%>%"
Execution halted