2017-10-04 43 views
0

我正在使用HighCharter,我正在嘗試創建時間序列圖,但日期未被拉到圖表上。以下是我目前的代碼。我沒有連續的時間系列,但我認爲這不應該在問題中發揮作用。R 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張圖片AUSPFinal 2)圖

的輸出的 1)輸出

AUSPFinal

HighCharter Graph

更新基於請求,下面是從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

用'hc_xAxis(類型= 「日期時間」,dateTimeLabelFormats =列表(天= '%B%d'))嘗試' –

+0

HI @MarcoSandri,改變圖形以以毫秒爲單位。即00:00:00,160至00:00:00.200。我沒有檢查課程,「Group.1」是「日期」。 – Kevin

+0

@MarcoSandri由於機密信息,我無法發佈'ShipmentsYear',但是我做過'HighCharter'正在使用的'AUSPFinal' – Kevin

回答

1
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 

enter image description here

+0

完美的作品!謝謝! – Kevin