Strings as tick values for rCharts NVD3 line chart
Asked Answered
E

0

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?

Earache answered 4/8, 2014 at 14:5 Comment(3)
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)Furlani
@Furlani thanks for the tip! This might just work for my purposes. Will test it out.Earache
if this does not work, please reply and I will work something up.Foremost

© 2022 - 2024 — McMap. All rights reserved.