rCharts-如何向NVD3图表添加轴标签和标题

13

我正在探索 rCharts。在添加Y轴标签和标题时遇到了困难。我是rCharts的新手。

这是我的样本代码

require(rCharts)
n2 <- nPlot(Hours ~ Month, group = "Task", data = cars, type = "multiBarChart",
height = 900, width = 1110)
n2$xAxis(axisLabel = 'Year and Month')
n2
请帮忙。

这是我的示例代码:
n2 <- nPlot(Hours ~ Month, group = "Task", data = cars, type = "multiBarChart",height = 900, width = 1110) n2$xAxis(axisLabel = 'Year and Month') n2 -- 我还需要为条形图添加Y轴和标题。
- user3047471
希望你不介意我编辑了你的问题,以使其更易读。 - dickoa
1个回答

13

回答补充了标题示例 2013-12-05

我不记得为什么会出现 nvd3rCharts 的这种情况,但我们在这个问题中发现了这个现象。那个问题中提出的方法是有效的,但使用 margin 可能是更可靠的方法。我准备了一个快速示例来展示这两种方法。请告诉我这是否有效。

      require(rCharts)

      df <- data.frame(x=1:20,y=runif(n=20))

      n1 <- nPlot(
        y~x,
        data=df,
        type="multiBarChart"
      )
      n1$yAxis( axisLabel = "Randomness" )

      #nvd3 draws the label but falls outside the bounds
      #so two ways to fix

      #best way I believe is to set the margin to allow room
      #nvd3 draws at -63, so something bigger than 63
      n1$chart(margin = list(left = 100))
      n1


      #second way as discussed here
      #https://github.com/ramnathv/rCharts/issues/102
      n1$yAxis( axisLabel = "Randomness", width = 40 )
      n1

现在让我们添加一个标题

有几种方法可以实现这一点。我目前更喜欢使用带有rCharts的脚本模板。以下是两个示例。在rCharts div中插入了一个<h3>元素。如果您想查看它们的工作原理,这两个模板位于此存储库中。

      #for a local template something like this
      #n1$templates$script <- "./chartWithTitle.html"
      n1$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
      n1$set(title = "rCharts + nvd3 Power")
      n1

      #using some css style from http://tympanus.net/codrops/2012/11/02/heading-set-styling-with-css/
      #put in a different template 
      n1$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle_styled.html"
      n1

太好了!我选择了第二种方法,它起作用了。我仍然无法添加标题,并且需要您的帮助在标题中加入加粗的帮助。 - user3047471
1
我不确定NVD3是否支持标题。 - Ramnath
非常感谢。我很欣赏您的快速回应。 - user3047471
我使用rCharts脚本模板添加了两个标题示例。 - timelyportfolio
了不起的贡献者们!!再次感谢。 - user3047471
@timelyportfolio,非常感谢,这个很好用。但是我无法在shiny应用程序中使用它,有什么想法吗?我应该发布一个带有gist的问题吗? - PatrickT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接