rcharts nvd3 linechart with categorical x axis
Asked Answered
F

1

2

How can I a draw a linechart with rcharts using nvd3 and a categorical xaxis? What I'm trying to do would look with ggplot2 like this:

library(ggplot2)
dat <- data.frame(expand.grid(group = letters[1:3], x = letters[4:6]), y = rnorm(9))
ggplot(dat, aes(x = x, y = y, group = group, color = group)) + geom_line()

I tried:

library(rCharts)
nPlot(y ~ x, group = 'group', data = dat, type = 'lineChart')

I know that I could change x to a numeric variable, but then the xaxis labels would not be correctly written.

Fancywork answered 25/8, 2014 at 8:26 Comment(2)
I know this is an old question. What I would like to know is: why would you ever want this? I cannot think of a single sensible example of a line chart with a categorical axis. I'd like to know if you have an example. Thanks in advance.Pled
It makes sense when it is on a ordinal scale.Fancywork
S
1

is this what you are after?

foo <- hPlot(x = "x", y = "y", groups = "group", data = dat, type = "line")
foo
Soil answered 25/8, 2014 at 8:41 Comment(7)
Yes, but I'd like to do the same thing with the nvd3 library.Fancywork
I have been checking what kind of examples exist. All examples I have seen have continuous variables on the x-axis including date. I am not familiar with ndv3. But it seems to me that what you are looking for is achieved with hPlot.Soil
Thanks, but I cannot consider my question as fully answered because I was asking for doing it with nvd3. I want to have the specific style and behavior of nvd3.Fancywork
I am sorry that I cannot help you any more. But, this is what I could offer for now. Let's see if somebody else can give you a hand.Soil
after a pretty thorough review of the code, this appears nearly impossible with nvd3 since there is no way to override this line github.com/novus/nvd3/blob/master/src/models/line.js#L166 which converts to character to numeric. Unfortunately, also the group behind nvd3 does not seem to working on this project anymore. However, there has been some discussion on a community edition, so maybe there is hope. I know that you mentioned the attractive feature set and aesthetics of nvd3, but if you can consider something else, dimple also makes this easy.Goldarned
Sure, I thought it would be easy with chart.xScale( d3.scale.ordinal() ) but I discovered that nvd3 kept returning numeric which lead me to that one line of code which is not exposed to be overridden. Let us know if we can help on anything else.Goldarned
Thanks. I changed my mind and have now decided to go with the original suggestion of using the highcharts library. I found out that I can customize highcharts to get the specific style that I want.Fancywork

© 2022 - 2024 — McMap. All rights reserved.