簡體   English   中英

如何在Dygraphs中為R設置x軸以顯示月份

[英]How to set x-axis in dygraphs for R to show just month

. that starts on 2012-01-01 and ends 2012-12-31): 我有以下時間序列( 從2012-01-01開始,到2012-12-31結束):

       ts.rmean ts.rmax
2012-01-01 3.163478    5.86
2012-01-02 3.095909    4.67
2012-01-03 3.112000    6.01
2012-01-04 2.922800    5.44
2012-01-05 2.981154    5.21
2012-01-06 3.089167    5.26

我正在使用dygraph for R進行繪圖:

library(dplyr)
library(digraphs)
dygraph(data.in, main = "Title") %>%
  dySeries("ts.rmean", drawPoints = TRUE, color = "blue") %>%
  dySeries("ts.rmax", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
  dyHighlight(highlightSeriesOpts = list(strokeWidth = 3))

可以在x軸上顯示月份而不是月份年份(例如“1月12日”)? 而且,在情節的傳說中,而不是顯示月日(例如2012年1月1日)以顯示月日?

您可以使用javascript函數修改所有軸標簽並設置值的格式。

例如:

library(dygraphs)
library(xts)
library(htmlwidgets)

#the axis label is passed as a date, this function outputs only the month of the date
getMonth <- 'function(d){
               var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
               return monthNames[d.getMonth()];
               }'

#the x values are passed as milliseconds, turn them into a date and extract month and day
getMonthDay <- 'function(d) {
                var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                date = new Date(d);
                return monthNames[date.getMonth()] + " " +date.getUTCDate(); }'

#set the value formatter function and axis label formatter using the functions above
#they are wrapped in JS() to mark them as javascript    
dygraph(data.in, main = "Title") %>%
        dySeries("ts.rmean", drawPoints = TRUE, color = "blue") %>%
        dySeries("ts.rmax", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
        dyHighlight(highlightSeriesOpts = list(strokeWidth = 3)) %>%
        dyAxis("x",valueFormatter=JS(getMonthDay), axisLabelFormatter=JS(getMonth))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM