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?
mydata$month
as Dates and then addp$xAxis(tickFormat="#!function(d) {return d3.time.format('%b')(new Date( d * 86400000 ));}!#")
? (via) – Furlani