install.packages ( "plotly" )
library( plotly)
# Sample data
x <- 1 : 10
y1 <- x^ 2
y2 <- x^ 3
linetype <- c( rep( "solid" , 5 ) , rep( "dash" , 5 ) )
color <- c( rep( "blue" , 5 ) , rep( "red" , 5 ) )
# Create the plot
p <- plot_ly(
data = data.frame ( x, y1, y2, linetype, color) ,
type = "scatter" ,
mode = "lines"
) %>%
# First trace with color and linetype mapping
add_trace(
x = ~x,
y = ~y1,
line = list( dash = ~linetype) ,
legendgroup = ~linetype, # Group by linetype
linetype = ~linetype,
color = I( "black" ) , # Set a static color so color doesn't create a new legend
showlegend = TRUE # Only show legend for linetype
) %>%
# Second trace for y2 with color and linetype mapping
add_trace(
x = ~x,
y = ~y2,
line = list( dash = ~linetype) ,
legendgroup = ~linetype,
linetype = ~linetype,
color = I( "black" ) , # Same static color to avoid color legend entries
showlegend = FALSE # Hide color legend
)
# Display the plot
p
aW5zdGFsbC5wYWNrYWdlcygicGxvdGx5IikKbGlicmFyeShwbG90bHkpCgojIFNhbXBsZSBkYXRhCnggPC0gMToxMAp5MSA8LSB4XjIKeTIgPC0geF4zCmxpbmV0eXBlIDwtIGMocmVwKCJzb2xpZCIsIDUpLCByZXAoImRhc2giLCA1KSkKY29sb3IgPC0gYyhyZXAoImJsdWUiLCA1KSwgcmVwKCJyZWQiLCA1KSkKCiMgQ3JlYXRlIHRoZSBwbG90CnAgPC0gcGxvdF9seSgKICBkYXRhID0gZGF0YS5mcmFtZSh4LCB5MSwgeTIsIGxpbmV0eXBlLCBjb2xvciksCiAgdHlwZSA9ICJzY2F0dGVyIiwKICBtb2RlID0gImxpbmVzIgopICU+JQogICMgRmlyc3QgdHJhY2Ugd2l0aCBjb2xvciBhbmQgbGluZXR5cGUgbWFwcGluZwogIGFkZF90cmFjZSgKICAgIHggPSB+eCwKICAgIHkgPSB+eTEsCiAgICBsaW5lID0gbGlzdChkYXNoID0gfmxpbmV0eXBlKSwKICAgIGxlZ2VuZGdyb3VwID0gfmxpbmV0eXBlLCAg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