【问题标题】:Plotly - Plot 2 Y Axes With Time SeriesPlotly - 用时间序列绘制 2 个 Y 轴
【发布时间】:2017-07-14 09:54:33
【问题描述】:

我正在尝试使用 r plotly 绘制具有以下功能的图表:

  1. 日期对象为 X 变量
  2. 一个图表中的 2 个折线图,带有 2 个 Y 轴:一个在左侧,另一个在右侧

    Date              Amount1          Amount2
    2/1/2017          19251130        21698.94
    2/2/2017          26429396        10687.37
    2/5/2017            669252            0.00
    2/6/2017          25944054        11885.10
    2/7/2017          27895562        14570.39
    2/8/2017          20842279        20080.56
    2/9/2017          25485527         9570.51
    2/10/2017          17008478        14847.49
    2/11/2017            172562            0.00
    2/12/2017            379397          900.00
    2/13/2017          25362794        18390.80
    2/14/2017          26740881        11490.94
    2/15/2017          20539413        22358.26
    2/16/2017          22589808        12450.45
    2/17/2017          18290862         3023.45
    2/19/2017           1047087          775.00
    2/20/2017           4159070         4100.00
    2/21/2017          28488401        22750.35
    

我使用的代码是:

      ay <- list(
    #tickfont = list(color = "red"),
    overlaying = "y",
    side = "right"
  )

  p <- plot_ly() %>%
    add_lines(x = df$Date, y = df$Amount1, name = "Amount1",type = "scatter", mode = "lines") %>%
    add_lines(x = df$Date, y = df$Amount2, name = "Amount2", yaxis = "y2",type = "scatter", mode = "lines") %>%
    layout(
      title = "Chart Summary", yaxis2 = ay,
      xaxis = list(title="Date")
    )

输出图表看起来不错,但 X 轴上的日期间隔看起来很糟糕。我想知道解决这个问题的方法是什么,如果我想使用上面的数据在一个图表中显示 2 个直方图,那么最佳方法是什么?

感谢您的帮助!

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    您的Date 列是string 还是date

    如果是string,则将其转换为date,并让 Plotly 处理。

    df$Date <- as.Date(df$Date , "%m/%d/%Y")
    

    完整代码

    library('plotly')
    
    txt <- "Date    Amount1 Amount2
    2/1/2017    19251130    21698.94
    2/2/2017    26429396    10687.37
    2/5/2017    669252  0
    2/6/2017    25944054    11885.1
    2/7/2017    27895562    14570.39
    2/8/2017    20842279    20080.56
    2/9/2017    25485527    9570.51
    2/10/2017   17008478    14847.49
    2/11/2017   172562  0
    2/12/2017   379397  900
    2/13/2017   25362794    18390.8
    2/14/2017   26740881    11490.94
    2/15/2017   20539413    22358.26
    2/16/2017   22589808    12450.45
    2/17/2017   18290862    3023.45
    2/19/2017   1047087 775
    2/20/2017   4159070 4100
    2/21/2017   28488401    22750.35"
    df$Date <- as.Date(df$Date , "%m/%d/%Y")
    ay <- list(
      #tickfont = list(color = "red"),
      overlaying = "y",
      side = "right"
    )
    
    p <- plot_ly() %>%
      add_lines(x = df$Date, y = df$Amount1, name = "Amount1",type = "scatter", mode = "lines") %>%
      add_lines(x = df$Date, y = df$Amount2, name = "Amount2", yaxis = "y2",type = "scatter", mode = "lines") %>%
      layout(
        title = "Chart Summary", yaxis2 = ay,
        xaxis = list(title="Date", ticks=df$Date)
      )
    p
    

    【讨论】:

    • 谢谢!这样可行。我想我错过了 ,因此 x 轴上的刻度显示不正确。
    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多