首页 文章

R Plotly - 混乱线图

提问于
浏览
1

我想在剧情中绘制时间序列 . Dataframe有两列,日期格式为date,odrers为整数,例如:

order_created   margin
2017-01-01      7004
2017-02-01      4424
2017-03-01      3792
2017-04-01      6031
2017-05-01      7561
2017-06-01      6903
2017-07-01      6687
2017-08-01      3283
2017-09-01      6683
2017-10-01      4781
2017-11-01      6960

当我使用type =“scatter”&fill ='tozeroy'绘制数据时,我得到了完整的混乱:

plot_ly(somedata, 
        x = ~order_created, 
        y = ~margin, 
        name = 'Total margin', 
        type = 'scatter', 
        mode = 'none', 
        fill = 'tozeroy')

enter image description here

当我使用相同的数据和代码,但将类型更改为“bar”时,它会创建正确的图:

plot_ly(somedata, 
    x = ~order_created, 
    y = ~margin, 
    name = 'Total margin', 
    type = 'bar', 
    mode = 'none', 
    fill = 'tozeroy')

enter image description here

有关如何生成正确的线(填充区域)图表的任何建议吗?

谢谢

1 回答

  • 0

    plotly 4.7.1 ,这对我来说很好:

    # reproducible example!
    somedata <- read.table(text=
    "order_created   margin
    2017-01-01      7004
    2017-02-01      4424
    2017-03-01      3792
    2017-04-01      6031
    2017-05-01      7561
    2017-06-01      6903
    2017-07-01      6687
    2017-08-01      3283
    2017-09-01      6683
    2017-10-01      4781
    2017-11-01      6960", header=TRUE,
    colClasses = c('Date','numeric'))
    
    library(plotly)
    (p <- plot_ly(somedata, 
            x = ~order_created, 
            y = ~margin, 
            name = 'Total margin', 
            type = 'scatter', 
            mode = 'none', 
            fill = 'tozeroy'))
    
    plotly_IMAGE(p, format = "png", out_file = "output.png")
    

    enter image description here

相关问题