【问题标题】:rCharts formattingrCharts 格式化
【发布时间】:2015-10-05 05:39:01
【问题描述】:

我对@9​​87654321@ 格式有点苦恼,这是我演示的一部分,我需要使用Highcharts,它是闪亮/反应性的一部分。我不能改变这些规则。 我想将我的 h1 格式化为类似于 hPlot 的评论,所以我将在 x 上使用我的 YYYYMM。我在互联网上查过,但所有这些信息对我来说太技术性了。这件作品我没有时间资源。

library(rCharts)

x <- data.frame(PRODUCT=factor(c("ALPHA", "ALPHA","BRAVO","ALPHA","ALPHA")), 
                YYYYMM= factor(c("2/1/2015","3/1/2015","4/1/2015","5/1/2015","6/1/2015")),
                MEM_COUNT=c(44,22,37,76,97))
## none of this formatting work
x$YYYYMM <- as.Date(x$YYYYMM)
x$YYYYMM = strftime(strptime(x$YYYYMM, format = "%m/%d/%Y"), format = "%b/%y") 

###  hPlot(MEM_COUNT ~ YYYYMM, data=x, type="line")  #<@>< good sample!!

### output$chart <- renderChart2({
  h1 <- Highcharts$new()
  h1$chart(type = "spline")
  h1$series(data = x$MEM_COUNT, dashStyle = "longdash")
  h1$legend(symbolWidth = 80)
  h1$xAxis(Title= list(text="Months"),type="datetime")
  h1
###})      

【问题讨论】:

    标签: r rcharts


    【解决方案1】:

    Mario,第一步是将日期更改为毫秒,不要切换到科学计数法,因为 Highcharts/javascript 期望日期/时间以毫秒为单位。

    options(scipen = 13)
    x$YYYYMM <- as.numeric(as.POSIXct(x$YYYYMM, format='%m/%d/%Y')) * 1000 
    

    现在,您可以构建图表了。使用您的示例数据,我们将绘制按 PRODUCT 分组的 MEM_COUNT 与 YYYYMM。 (因为产品“Alpha”只有 1 个数据点,所以没有多大意义,但无论如何..)

    h1 <- Highcharts$new()
    h1 <- hPlot(MEM_COUNT ~ YYYYMM, data = x, 
          group = 'PRODUCT', 
          type = "spline" 
          )
    h1$xAxis(type="datetime", labels = list(
      format = '{value:%b-%Y}'))
    h1
    

    通过更改“标签”值,您可以根据自己的喜好设置 x 轴的格式。

    【讨论】:

    • 谢谢,hvollmeier !
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多