Strings as tick values for rCharts NVD3 line chart

偶尔善良 提交于 2019-12-12 05:03:12

问题


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?

来源:https://stackoverflow.com/questions/25120598/strings-as-tick-values-for-rcharts-nvd3-line-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!