1

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 (i.e. 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. Thanksenter image description here

Angelo
  • 1,594
  • 5
  • 17
  • 50

1 Answers1

2

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)
)
Andreas
  • 210
  • 2
  • 9
  • Thank you very much Andreas, your solution actually worked. May I ask what parameter should I use to have dotted lines? Thanks again for your help, much appreciated. – Angelo Jul 01 '19 at 12:31
  • 1
    Thanks for asking. I took a second look at my answer and realized the table inputs for colors and line types (such as 'Red' and 'solid') were being read in as factors by plot_ly. Per this question (https://stackoverflow.com/questions/44973625/plotly-how-to-specify-symbol-and-color-based-on-value?rq=1), I found you can use the function 'I' to get plot_ly to read them literally. And FYI, the allowed values for linetypes are 'solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot'. I have updated my answer accordingly. – Andreas Jul 01 '19 at 15:38
  • that's super helpful. Thank you Andreas – Angelo Jul 01 '19 at 21:36
  • Hi, apologizes for asking another question. I used your solution with the function "I". That works nicely but that way all the lines inside the legend box are now called "blue, red, orange". Is there any way to customize the line names inside the legend box? – Angelo Jul 09 '19 at 13:55
  • @Angelo I've edited my answer to allow for custom names – Andreas Jul 09 '19 at 15:30
  • it works perfectly. You saved me twice. Hugely appreciated. – Angelo Jul 09 '19 at 17:41