如何更改hc_xAxis中的时间范围 - 以四分之一为单位的月份

时间:2018-05-04 11:26:35

标签: r highcharts

我想在HighCharts中绘制时间序列为四分之一的时间序列。但在X轴上我只有月度。如何转换季度数据?我在这篇文章enter link description here中找到了我的问题的答案,但我不知道如何在R中实现这一点。

xAxis: {
        alternateGridColor: '#FAFAFA',
        labels: {
            style: {
                fontSize: '9px',
                width: '175px'
            },
            formatter: function () {
                var s;
                if (Highcharts.dateFormat('%b', this.value) == 'Jan') {
                    s = s + "Q1"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Apr') {
                    s = s + "Q2"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Jul') {
                    s = s + "Q3"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Oct') {
                    s = s + "Q4"
                };
                s = s + " " + Highcharts.dateFormat('%Y', this.value);
                return s;
            }
        },
        tickInterval: 31536000000,
        type: 'datetime'
    }

2 个答案:

答案 0 :(得分:0)

library("lubridate") # 
library("readxl")
library("zoo") #
library("xts") #
library("dplyr") # 
library("ggplot2") # 
library("gridExtra") # Multiplot
library("forecast")
library("tseries") #для Arima
library("TTR") # SMA
library("fUnitRoots") #tests for stationarity
library("xlsx")
library("RODBC") # xls Excel <- odbcConnectExcel
library("Quandl")
library("highcharter")
library("quantmod")
library("XLConnect")
library("xlsx")
library("js") #Tools for Working with JavaScript in R

# load data
tmpgdplev = tempfile(fileext = ".xlsx")
download.file(url = "https://www.bea.gov/national/xls/gdplev.xlsx", destfile = tmpgdplev, mode="wb")
mydatagdplev<-read.xlsx(tmpgdplev, 1, startRow = 6)

RealGDPQ<-ts(removeNA(mydatagdplev[1:nrow(mydatagdplev),6]), frequency=4, start=c(1947,1))

prirost<-function(x)
{
  d<-diff(x)
  res<-NULL
  for(i in 1:length(d)) 
  {res<-append(res, d[i]/x[i])}
  return(res)
}

dRGDP<-prirost(RealGDP)

dRGDPQ<-prirost(RealGDPQ)
round(dRGDP,4)
round(dRGDPQ*100,2)

prirostQ<-function(x)
{
  d<-diff(x,lag = 4)
  res<-NULL
  for(i in 1:length(d)) 
  {res<-append(res, d[i]/x[i])}
  return(res)
}

dRGDPQ2<-prirostQ(RealGDPQ)
round(dRGDPQ2*100,2)

dQRGDPQ<-(dRGDPQ+1)^4-1
round(dQRGDPQ*100,2)

k<-NULL
for(i in 1:NROW(removeNA(mydatagdplev[ ,1]))) 
{k<-append(k, as.character(mydatagdplev[i,1]))}
dpc<-years(k)
n<-NA
n1<-c(NA,NA,NA,NA)

QRealGDPtable<-data.frame(Qdate, RealGDPQ, round(append(n,dRGDPQ)*100,2 ), round(append( n1,dRGDPQ2)*100,2 ), round(append(n,dQRGDPQ)*100,2 ))
colnames(QRealGDPtable)<-c("date", "QRealGDP", "Growth","QQGrowth", "annualGrowthRate")
Qdate<-as.yearqtr(1947 + seq(0, nrow(mydatagdplev)-1)/4)
#### This my working time siries
QQRGDPts<-ts(QRealGDPtable$QQGrowth, start=c(1947,1), frequency=4)

QQRGDP <- highchart(type = "stock") %>% 
  hc_title(text = "Q U.S. GDP ") %>% 
  hc_subtitle(text = "Source: U.S. Bureau of Economic Analysis",  align = "left") %>%
  hc_add_series(QQRGDPts, type="column")
  QQRGDP

答案 1 :(得分:0)

我的问题在于尝试在xAxis上干净地显示季度数据。我理解我必须在hc_xAxis()中使用函数formatter和tickInterval,并且可能正确使用datetime_to_timestamp()(我使用ts(频率= 4) - 这个函数在highcharter {}中不起作用)。在上面的帖子中,这个问题在javascript中解决了,但我不知道如何在R中实现它。