Tobias D
Tobias D

Reputation: 375

Plotly r, line style by variable

I am trying to make a scatter plot where the line style is specified by a factor variable, just as one does with colors, but I can't get it to work.

library(plotly)
df <- data.frame(x=rnorm(20),y=rnorm(20),col=c(rep(1,10),rep(2,10)),dash=c(1,2))
df <- df[order(df[,1]),]
plot_ly(data=df,x=~x,y=~y,type="scatter",mode="lines",color=~as.factor(col),line=list(dash=~as.factor(dash)))

Upvotes: 2

Views: 1126

Answers (1)

Tobias D
Tobias D

Reputation: 375

I found the answer myselfes:

plot_ly(data=df,x=~x,y=~y,type="scatter",mode="lines",color=~as.factor(col),linetype=~as.factor(dash))

Upvotes: 1

Related Questions