简体   繁体   English

带有自定义线型的R条多线图

[英]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. 我可能有一个简单的R plotly问题,但是我花了大约一个小时来阅读stackoverflow中的问题,但我确实找不到我需要的东西。 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). 我需要/想要在数据框内指定此信息,然后选择包含此类信息的数据框列(即,请参阅我所附数据框中的“ LINE_STYLE_FACTOR”列)。 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. 使用plotly rPlotly r 通过线型检查了多个折线图 但是我找不到如何执行所需的操作。 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)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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