2014-07-09 59 views
1

考虑的情节,从rCharts/NVD3例子页rCharts格式轴标签:OUTPUTFORMAT

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

我试图让我的y轴显示百分比拉,所以我尝试了下面的代码(以下R: interactive plots (tooltips): rCharts dimple plot: formatting axis)但它返回一个空白页

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

我做了一些研究,得到了以下工作:

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

现在,我知道约0 javascript,并希望尽可能留在R中,至少在短期内。可能有人让我知道

  1. 为什么第一个方法是行不通的,而
  2. 无论第二种方法是去了解它
+2

'rCharts'由许多不同的API库组成,它们具有不同的API。第一种方法是用于dimple.js库。对于nvd3.js,javascript是必要的。 – jdharrison

+0

@jdharrison感谢您的提示!我想是时候让我拿起一些javascript ... – kevinykuo

+0

谢谢@jdharrison的答案。最终我们希望使这些一致,但现在不像我们想要的那么容易。 – timelyportfolio

回答

3

最好的方法,自答案1是在@jdharrison发表评论,我认为我会很快用一个完整的代码示例回答#2,其中注释展示了实现同一事物的不同方式。

#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) 
+0

这篇文章帮助我调整了由rCharts和nplot()函数生成的“multiBarChart”。我需要一个没有任何小数的刻度标记格式,因为我想在y轴上显示原始频率。这可以通过''pl2 $ yAxis(tickFormat =“#!d3.format(',。0f')!#”)来完成。 – swolf