1
votes

I have a monthly time series data set from 2013-07 to 2014-07 and I'm trying to plot a bar chart based on that data set in nPlot from rCharts. The plot looks all right however the ticks of x axis only shows 2013-09, 2013-12, 2014-03, 2014-06. So what can I do to change the ticks and show all the month (e.g. 2013-07, 2013-08, 2013-09, ..., 2014-07).

Suppose my rChart plot is called n1. Do I need to change something in n1$xAxis or n1$chart? If so how do I make this modification.

Thanks if there is any suggestions

1

1 Answers

2
votes

To accomplish this you will need a little Javascript. I tried to comment in the code what you might want to change. You could also supply an array of values in the tickValues.

library(rCharts)

data(economics, package = 'ggplot2')
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$xAxis(
  tickFormat = "#!function(d){return d3.time.format('%b %Y')(new Date(d*60*60*24*1000))}!#"
  #for everything
  #,tickValues = "#!data[0].values.map(function(v){return v[opts.x]})!#"
  #for something potentially more sensible
  ,tickValues = "#!data[0].values
      .map(function(v){
        return v[opts.x]
      })
      .filter(function(v){
        return d3.time.format('%m')(new Date(v*60*60*24*1000)) == '12' && +d3.time.format('%Y')(new Date(v*60*60*24*1000)) % 10 == 0
      })!#"
)
p6