1

Consider the plot, pulled from the rCharts/NVD3 examples page

p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6

I tried to get my y-axis to display percentages, so I tried the following code (following R: interactive plots (tooltips): rCharts dimple plot: formatting axis) but it returns a blank page

p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$yAxis(outputFormat = "%")
p6

I did some research and got the following to work:

p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$yAxis(tickFormat = "#! function(d) {return d*100 + '%' } !#")
p6

Now, I know about 0 javascript and want to stay in R as much as possible, at least in the short term. Could someone let me know

  1. Why the first approach doesn't work, and
  2. Whether the second approach is the best way to go about it
Community
  • 1
  • 1
kevinykuo
  • 4,600
  • 5
  • 23
  • 31
  • 2
    `rCharts` is composed of many different JavaScript libraries with different api's. The first approach is for the dimple.js library. For nvd3.js the javascript is necessary. – jdharrison Jul 09 '14 at 21:14
  • @jdharrison thanks for the tip! I guess it's time for me to pick up some javascript... – kevinykuo Jul 09 '14 at 21:16
  • thanks @jdharrison for the answer. eventually we would like to make these consistent but for now not as easy as we would like. – timelyportfolio Jul 10 '14 at 04:32

1 Answers1

3

Since the answer to 1 is in the comment by @jdharrison, I thought I would quickly answer #2 with a complete code sample with comments demonstrating the different ways to achieve the same thing.

  #turn off rstudio viewer
  #since we will be making lots of charts
  #we'll turn it back on at end
  oldviewer = options("viewer")
  options(viewer=NULL)

  data(economics, package = "ggplot2")

  #if we know we would like percent
  #and R is our friend but javascript is not
  #we could do some transformation in R
  economics$uempmed <- economics$uempmed/100

  p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
  p6
  #then we could use d3.format just like the dimple example
  #except dimple assumes the d3.format part so we just need "%"
  #see https://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#tickFormat
  #for more on d3.format
  #see https://github.com/mbostock/d3/wiki/Formatting#d3_format
  p6$yAxis( tickFormat = "#!d3.format('%')!#" )
  p6

  #however your method works fine also
  p6$yAxis(tickFormat = "#! function(d) {return d*100 + '%' } !#")
  p6

  #but let's say we did not do the above transform
  #dividing by 100
  #here is how we could do it in javascript
  data(economics, package = "ggplot2")
  p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
  p6$yAxis(tickFormat = "#! function(d) {
    //will change to .2% to show how to get two decimal
   return d3.format('.2%')(d/100)
  } !#")
  p6

  #turn rstudio viewer back on
  options(viewer = oldviewer)
timelyportfolio
  • 6,479
  • 30
  • 33
  • This post helped me a lot to adapt my "multiBarChart" generated by rCharts and the nplot() function. I needed a tick mark format without any decimals because I wanted to display raw frequencies on the y-axis. This can be done by ``pl2$yAxis(tickFormat = "#! d3.format(',.0f')!#")`` – swolf May 18 '16 at 14:18