hPlot R x轴标签和x-y对应

时间:2014-02-16 21:34:13

标签: r rcharts

我有以下数据并尝试使用hPlot

进行绘图
hPlot(Numbers~Date, data = df, group='Type', type = "line", radius=6)


     Date        Type      Numbers
 2014-01-05     Type-1          16
 2014-01-12     Type-1          82
 2014-01-12     Type-2           2
 2014-01-19     Type-1         177
 2014-01-26     Type-1         270
 2014-01-26     Type-2           3
 2014-02-02     Type-1         381
 2014-02-09     Type-1         461
 2014-02-09     Type-2           4

我将多个日期作为x轴,如下图所示。我还尝试了uniqueas.character,但x数据与y数据不对应。

enter image description here

1 个答案:

答案 0 :(得分:3)

以下是您的问题的解决方案。您可以查看图表以及代码here。我也在这里张贴

# don't switch to scientific notation, since we want date to be
# represented in milliseconds
options(scipen = 13)
dat = transform(df, Date2 = as.numeric(as.POSIXct(Date))*1000)

library(rCharts)
h1 <- hPlot(Numbers ~ Date2, data = dat, 
  group = 'Type', 
  type = "line", 
  radius=6
)
h1$xAxis(type = 'datetime', labels = list(
  format = '{value:%Y-%m-%d}'  
))
h1