rCharts-How to add axis lables and Headings to NVD3 chart
Asked Answered
I

1

13

I am exploriing rCharts. I got stuck while adding Lables to Y axis and Headings. I am new to rCharts.

This is my sample code

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

Please help.

Immortality answered 2/12, 2013 at 18:4 Comment(2)
This is my sample code:> n2 <- nPlot(Hours ~ Month, group = "Task", data = cars, type = "multiBarChart",height = 900, width = 1110) > n2$xAxis(axisLabel = 'Year and Month') > n2 -- I still need to add Y axis and Headings to Bar ChartImmortality
I hope that you don't mind that I edited your question to make it more readable.Drudgery
U
13

answer supplemented with title examples 2013-12-05

I cannot remember why nvd3 with rCharts does this but we have discovered this in this issue. The method suggested in that issue works, but using margin is probably the more robust method. I have put a quick example together of both ways. Let me know how this works.

      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

now let's add a title

There are a couple ways to achieve this. I currently prefer using a script template with rCharts. Here are two examples. An <h3> element is inserted in the rCharts div. The two templates live in this repo if you would like to see how they work.

      #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
Unchancy answered 2/12, 2013 at 18:45 Comment(6)
Wonderful! I choose second way and it did work. I still not able to add Header and I need your help adding help in Header with bold.Immortality
I am not sure if NVD3 supports titles.Stank
Thank you very much. I appreciate for your quick response.Immortality
I added two title examples above using rCharts script templates.Unchancy
Awesome contributors!! Thanks again.Immortality
@timelyportfolio, This works very well thanks. I couldn't get it to work inside a shiny app, any ideas? Should I post a question about this with a gist?Aweinspiring

© 2022 - 2024 — McMap. All rights reserved.