# your code goes herelibrary(plotly)
# Sample data
x <- 1:10
y1 <- x^2
y2 <- x^3
y3 <- x^3 + 20
linetype <- c(rep("solid", 5), rep("dash", 5), rep("dot",5))
color <- c(rep("blue", 5), rep("red", 5), rep("green",5))
# Create the plot
p <- plot_ly(
data = data.frame(x, y1, y2, y3, linetype, color),
type = "scatter",
mode = "lines",
x = ~x,
y = ~y1,
color = ~color, # Keep color mapping, but will not show on legend
linetype = ~linetype,
legendgroup = ~linetype, # Group by linetype for legend entries
showlegend = TRUE # Show legend for the first entry of each group (linetype)
) %>%
add_trace(
y = ~y2,
linetype = ~linetype,
color = ~color,
legendgroup = ~linetype,
showlegend = FALSE # Don't trigger a legend entry for subsequent lines with the same linetype
) %>%
add_trace(
y = ~y3,
linetype = ~linetype,
color = ~color,
legendgroup = ~linetype,
showlegend = FALSE # Don't trigger a legend entry for subsequent lines with the same linetype
)
# display the plot
p
IyB5b3VyIGNvZGUgZ29lcyBoZXJlbGlicmFyeShwbG90bHkpCgojIFNhbXBsZSBkYXRhCnggPC0gMToxMAp5MSA8LSB4XjIKeTIgPC0geF4zCnkzIDwtIHheMyArIDIwCmxpbmV0eXBlIDwtIGMocmVwKCJzb2xpZCIsIDUpLCByZXAoImRhc2giLCA1KSwgcmVwKCJkb3QiLDUpKQpjb2xvciA8LSBjKHJlcCgiYmx1ZSIsIDUpLCByZXAoInJlZCIsIDUpLCByZXAoImdyZWVuIiw1KSkKCiMgQ3JlYXRlIHRoZSBwbG90CnAgPC0gcGxvdF9seSgKICBkYXRhID0gZGF0YS5mcmFtZSh4LCB5MSwgeTIsIHkzLCBsaW5ldHlwZSwgY29sb3IpLAogIHR5cGUgPSAic2NhdHRlciIsCiAgbW9kZSA9ICJsaW5lcyIsCiAgeCA9IH54LAogIHkgPSB+eTEsCiAgY29sb3IgPSB+Y29sb3IsICMgS2VlcCBjb2xvciBtYXBwaW5nLCBidXQgd2lsbCBub3Qgc2hvdyBvbiBsZWdlbmQKICBsaW5ldHlwZSA9IH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