简体   繁体   中英

R multiple lines plotly chart with customized line types

I have probably a simple R plotly question but I spent about one hour reading questions in stackoverflow and I really can't find what I need. I have a dataframe (I will share a screenshot) with different columns used to create a multiple lines plotly chart. This is the code I use to create the plot:

plot_ly(data = df_final, x=~TENOR, y=~RATE) %>% add_trace(type='scatter',mode='lines', color=~LINE_NAME, colors = ~LINE_COL) %>%
    layout(title=paste0("Market data"),
           xaxis=list(title='Term (years)'),
           yaxis=list(title='Yield'))

it works amazing but I would like to have the option to choose if some lines will have to be dashed, dots, or solid lines as well as their width. I would need / want to specify this information inside the dataframe and choose the dataframe column that has such information (ie see the column "LINE_STYLE_FACTOR" in my attached dataframe). I checked Multiple line chart using plotly r and Plotly r, line style by variable but I can't find how to do what I need. The solution has to use plotly and not other charting solutions. Thanks 在此处输入图片说明

At least for the line types (dash vs line), you can you 'linetype':

library(dplyr)
library(plotly)

df = data.frame(xVals = rep(1:10,2),
                yVals = c(1:10, 2:11),
                myColor = c(rep('Red', 10), rep('Blue', 10)),
                myType = c(rep('solid', 10), rep('dot', 10)),
                myName = c(rep('FirstName', 10), rep('SecondName', 10)))
plot_ly(df, 
        x = ~xVals,
        y = ~yVals,
        color = ~I(myColor),
        name = ~myName,
        type = 'scatter',
        mode = 'lines',
        linetype = ~I(myType)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM