本文介绍了R HighCharter股票图未正确提取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with HighCharter and I am trying to create a timeseries graph but the dates are not being pulled onto the chart. Below is my current code. I do not have a continuous time series but I do not think that should play a role in the problem.

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)图形输出

Below are 2 pictures:1) output of AUSPFinal2) output of graph

根据请求进行更新,以下是 dput 的输出.

Update based on request, below is output from 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>)

推荐答案

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

这篇关于R HighCharter股票图未正确提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 02:27