简体   繁体   English

R Dygraphs从每月到每年的X轴粒度

[英]R Dygraphs X-axis granularity from monthly to yearly

I'm using dygraphs and would like to change the x axis granularity from daily to yearly. 我正在使用图表,并希望将x轴的粒度从每天更改为每年。 I have a few daily data points all in the format c("2009-01-01", "2010-01-01", "2011-01-01"), and the x axis ticks appears at Jan 2009, June 2009, Jan 2010, June 2010 I want the x axis to only appear as "2009, 2010, 2011" corresponding to data points that actually exist.. 我有一些日常数据点,其格式均为c(“ 2009-01-01”,“ 2010-01-01”,“ 2011-01-01”),并且x轴刻度出现在2009年1月,2009年6月,2010年1月,2010年6月我希望x轴仅显示为“ 2009、2010、2011”,与实际存在的数据点相对应。

I have the following code: 我有以下代码:

dygraph(hhinfl) %>% dyLegend(width=400, show="auto") %>% dyOptions(drawPoints=TRUE, pointSize=3) %>% dySeries("Household Inflation", color="#0174DF") %>% dySeries("date", color="#FFFFFF")%>% dySeries("Average Inflation", color="#DF0101") %>% dyAxis("y", label="Inflation Rate (%)")  %>% dyAxis("x", drawGrid = FALSE, axisLabelFormatter="function(d) { return d.getFullYear() }")

which returns the YEAR of each date on the x-axis, but that means there are multiple years like "Jan 2009, June 2009, Jan 2010, June 2010" becomes "2009, 2009, 2010, 2010" 会返回x轴上每个日期的YEAR,但这意味着存在多个年份,例如“ 2009年1月,2009年6月,2010年1月,2010年6月”变为“ 2009年,2009年,2010年,2010年”

Alternatively, another code shows: 另外,另一个代码显示:

dygraph(hhinfl) %>% dyLegend(width=400, show="auto") %>% dyOptions(drawPoints=TRUE, pointSize=3) %>% dySeries("Household Inflation", color="#0174DF") %>% dySeries("date", color="#FFFFFF")%>% dySeries("Average Inflation", color="#DF0101") %>% dyAxis("y", label="Inflation Rate (%)")  %>% dyAxis("x", drawGrid = FALSE, ticker= " function (a, b, pixels, opts, dygraph, vals) {return Dygraph.getDateAxis(a, b, Dygraph.YEARLY, opts, dygraph);}", axisLabelFormatter="function(d) { return d.getFullYear() }")

Does not return any graph at all, except for the y-axis. 除了y轴外,根本不返回任何图形。

How may I resolve this issue? 我该如何解决这个问题?

hhinfl is an xts file constructed as follows: hhinfl是一个xts文件,其结构如下:

dateseq<- seq(from=as.Date("2010-01-01"),to=as.Date("2013-12-31"),by="year")
household<- c(2.3, 2.4, 2.5, 3.1)
avg<- c(2.5, 2.6, 2.7, 3.1)

hhinfl<- data.frame(date=dateseq, hh=household, average=avg)
colnames(hhinfl)<- c("date", "Household Inflation", "Average Inflation")
hhinfl<-xts(hhinfl, order.by=hhinfl$date)  

So close! 很近! Before I noticed your "Alternatively, another code shows:" above, I wrote code essentially identical to yours (cribbing from http://jsfiddle.net/kaliatech/P8ehg/ ) and got the same blanked-out chart. 在我注意到您的“或者,另一个代码显示:”上面的代码之前,我编写了基本上与您相同的代码(从http://jsfiddle.net/kaliatech/P8ehg/抄录),并得到了相同的图表。

Then I stumbled into https://github.com/danvk/dygraphs/blob/master/src/dygraph-tickers.js#L221 and saw that the parameter should be ANNUAL , not YEARLY . 然后我偶然发现https://github.com/danvk/dygraphs/blob/master/src/dygraph-tickers.js#L221并看到参数应该是ANNUAL ,而不是YEARLY

So here's what turns out well for me: 所以这对我来说很不错:

dygraph(hhinfl) %>% 
    dyLegend(width=400, show="auto") %>% 
    dyOptions(drawPoints=TRUE, pointSize=3) %>% 
    dySeries("Household Inflation", color="#0174DF") %>% 
    dySeries("date", color="#FFFFFF") %>% 
    dySeries("Average Inflation", color="#DF0101") %>% 
    dyAxis("y", label="Inflation Rate (%)")  %>% 
    dyAxis("x", drawGrid = FALSE) %>%
    dyAxis("x", axisLabelFormatter="function(d) { return d.getFullYear() }") %>%
    dyAxis("x", ticker="function(a, b, pixels, opts, dygraph, vals) {
                            return Dygraph.getDateAxis(a, b, Dygraph.ANNUAL, opts, dygraph)
                            // or TWO_HOURLY or SIX_HOURLY...
                        }")

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

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