0

I'm trying to use display categorical levels in the x-axis of my line chart. Say I have a data frame

mydata <- data.frame(time = c("10am", "12pm", "3pm"), month = c("JAN", "JUL", "DEC"), value = 3:5)

and I want to plot value ~ time or value ~ month. I could first create a variable that puts time on a linear scale, e.g.

mydata$time_linear <- 1:3

then try to plot

library(rCharts)
p <- nPlot(value ~ time_linear, data = mydata, type = "lineChart")
p$chart(useInteractiveGuideline = TRUE)
p

but I don't know how to force the labels.

I have looked at How to set a nvd3 axis to use strings instead of numerical values ? and nvd3 line chart with string values on x-axis but am not sure how it works in the context of rCharts. It seems like one way would be to define an array in javascript with the label names and index accordingly, and another would be to define a function with a bunch of if/else statements. The former method seems more elegant but I don't know how to conjure up an array in the right scope, and the latter would involve a nasty long anonymous function in tickFormat

What's the best way to go about this?

Community
  • 1
  • 1
kevinykuo
  • 4,600
  • 5
  • 23
  • 31
  • 2
    What if you define `mydata$month` as Dates and then add `p$xAxis(tickFormat="#!function(d) {return d3.time.format('%b')(new Date( d * 86400000 ));}!#")`? ([via](https://github.com/ramnathv/rCharts/blob/master/inst/libraries/nvd3/examples.R)) – lukeA Aug 04 '14 at 14:48
  • @lukeA thanks for the tip! This might just work for my purposes. Will test it out. – kevinykuo Aug 04 '14 at 15:23
  • if this does not work, please reply and I will work something up. – timelyportfolio Aug 04 '14 at 21:11

0 Answers0