我正在使用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)图的输出
根据请求进行更新,以下内容从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>)
答案 0 :(得分: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