首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rCharts格式化轴标签: outputFormat

rCharts格式化轴标签: outputFormat
EN

Stack Overflow用户
提问于 2014-07-09 21:07:08
回答 1查看 954关注 0票数 1

请考虑从rCharts/NVD3 3示例页面中提取的绘图。

代码语言:javascript
复制
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6

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

代码语言:javascript
复制
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$yAxis(outputFormat = "%")
p6

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

代码语言:javascript
复制
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$yAxis(tickFormat = "#! function(d) {return d*100 + '%' } !#")
p6

现在,我知道大约0的javascript,并希望尽可能多地停留在R,至少在短期内。有人能告诉我吗

  1. 为什么第一种方法不起作用
  2. 第二种方法是否是最好的方法?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-10 04:49:14

由于对1的答案在@jdharrison的注释中,我想我应该用完整的代码示例快速地回答#2,并给出说明实现同一目标的不同方法的注释。

代码语言:javascript
复制
  #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)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24663820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档