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