Mario Trento
Mario Trento

Reputation: 523

rCharts formatting

I'm bit struggling with rCharts formatting, it's part of my part of presentation and I need to go with Highcharts, and it's part of shiny/reactive. I can not change these rules. I would like to format my h1 to be similar to that commented hPlot so I'll have my YYYYMM on x. I've checked on Internet but all this info is too technical for me. I don't have time resources for this piece.

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
###})      

Upvotes: 0

Views: 334

Answers (1)

hvollmeier
hvollmeier

Reputation: 2986

Mario, the first step is to change the Date to milliseconds and don’t switch to scientific notation, as Highcharts/javascript expects the date/time represented in milliseconds.

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

Now, you can build the chart. Using your example data we will plot the MEM_COUNT vs. YYYYMM, grouped by PRODUCT. ( as product ‘Alpha’ has only 1 data point it doesn’t make much sense, but anyway..)

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

By changing the ‘labels’ values you can format the x-axis to your liking.

enter image description here

Upvotes: 1

Related Questions