0

我正在使用 HighCharter,我正在尝试创建一个时间序列图,但日期没有被拉到图表上。以下是我当前的代码。我没有连续的时间序列,但我认为这不应该在问题中起作用。

library (shiny)
library (shinydashboard)
library (date)
library (tidyr)
library (dplyr)
library (data.table)
library (zoo)
library (tibble)
library (scales)
library (highcharter)
library (quantmod)
library (RODBC)

ShipmentsYear <- #Pulling data from SQl Server

AUSP <- aggregate(ShipmentsYear$Sales.Net.Value, by = list(ShipmentsYear$Shipment.Date),FUN=mean)
AUSP <- data.table(AUSP)
AUSP <- AUSP[ ,Index:=1:.N]

Mean <- rollapply(AUSP$x,30, FUN=mean, fill=NA, partial = TRUE, align = 'right')
Mean <- data.table(Mean)
Mean <- Mean[ ,Index:=1:.N]

AUSPFinal <- merge(AUSP,Mean,by="Index")

hc<-
highchart(type = "stock") %>%
  hc_yAxis(min = 0, title = list(text = "Average Unit Selling Price (AUSP)")) %>%
  hc_xAxis(type = 'datetime', labels = list(format = '{value:%b %d}')) %>%
  hc_add_series(name = "Average Day", data=AUSPFinal$x, type = "line", color = "#2670FF", marker = list(radius = 2), alpha = 0.5) %>%
  hc_add_series(name = "Rolling Mean", data=AUSPFinal$Mean, color = "#FF7900") %>%
  hc_tooltip(valueDecimals = 0) %>%
  hc_rangeSelector(enabled = FALSE)

hc

下面是2张图片:1)AUSPFinal的输出2)图表的输出

澳大利亚决赛

HighCharter 图表

根据请求更新,以下是来自 的输出dput

> dput(AUSPFinal[1:20,])
structure(list(Index = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20), Group.1 = structure(c(17169, 
17170, 17171, 17172, 17175, 17176, 17177, 17178, 17179, 17180, 
17182, 17183, 17184, 17185, 17186, 17187, 17189, 17190, 17191, 
17192), class = "Date"), x = c(4069, 4903, 3427, 4357, 3703, 
2888, 3034, 2539, 3618, 4001, 4190, 3553, 3915, 3156, 3943, 5948, 
3974, 2707, 3275, 4042), Mean = c(4069, 4486, 4133, 4189, 4092, 
3891, 3769, 3615, 3615, 3654, 3703, 3690, 3707, 3668, 3686, 3828, 
3836, 3774, 3747, 3762)), .Names = c("Index", "Group.1", "x", 
"Mean"), sorted = "Index", class = c("data.table", "data.frame"
), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x042a24a0>)
4

1 回答 1

2
AUSPFinal <- 
structure(list(Index = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20), Group.1 = structure(c(17169, 
17170, 17171, 17172, 17175, 17176, 17177, 17178, 17179, 17180, 
17182, 17183, 17184, 17185, 17186, 17187, 17189, 17190, 17191, 
17192), class = "Date"), x = c(4069, 4903, 3427, 4357, 3703, 
2888, 3034, 2539, 3618, 4001, 4190, 3553, 3915, 3156, 3943, 5948, 
3974, 2707, 3275, 4042), Mean = c(4069, 4486, 4133, 4189, 4092, 
3891, 3769, 3615, 3615, 3654, 3703, 3690, 3707, 3668, 3686, 3828, 
3836, 3774, 3747, 3762)), .Names = c("Index", "Group.1", "x", 
"Mean"), sorted = "Index", class = c("data.table", "data.frame"
), row.names = c(NA, -20L)) 

library(highcharter)

hc <-
highchart(type = "stock") %>%
  hc_add_series_times_values(AUSPFinal$"Group.1", AUSPFinal$x, type = "line", color = "#2670FF", marker = list(radius = 2), alpha = 0.5) %>%
  hc_add_series_times_values(AUSPFinal$"Group.1", AUSPFinal$Mean, color = "#FF7900") %>%
  hc_yAxis(min = 0, title = list(text = "Average Unit Selling Price (AUSP)")) %>%
  hc_xAxis(type = 'datetime', labels = list(format = '{value:%b %d}')) %>%
  hc_tooltip(valueDecimals = 0) %>%
  hc_rangeSelector(enabled = FALSE)
hc

在此处输入图像描述

于 2017-10-04T19:55:57.107 回答