2017-02-23 3 views
0

나는 다음과 같은 기능을하고있다 차트 플롯 plotly R를 사용하는 것을 시도하고있다 : 하나 개의 차트 Plotly - 플롯이 Y 축으로 시계열

  • 2 라인 플롯 X-변수로

    1. Date 객체를 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 
      

    및 I를 사용하는 코드의 다른이다

    01,235 16,
     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") 
        ) 
    

    Chart

    출력 차트는 잘 보이지만 X 축에 날짜 간격 나쁜 찾고 있습니다. 위의 데이터를 사용하여 하나의 차트에 2 개의 히스토그램을 갖고 싶다면이를 수행하는 최적의 방법은 무엇입니까?

    도움 주셔서 감사합니다.

  • 답변

    0

    Datestring 또는 date입니까?

    string 인 경우 date으로 변환하여 Plotly가 처리하도록하십시오.

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

    Autofix 전체 코드

    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 
    
    +0

    감사합니다! 그거야. 나는 를 그리워 X 축의 틱이 잘못 표시되도록했다. –

    관련 문제