0

I have a problem in adding labels in ?nPlot(). For example:

```{r nvd3plot, echo=FALSE,results='asis'}
set.seed(9485)
dat<- data.frame(Gene_Name= LETTERS[1:15], Value1= sample(-8:20,15,replace=TRUE),Value2=sample(-6:10,15,replace=TRUE),stringsAsFactors=FALSE) 
library(rCharts)
r1<- nPlot(Value1~Value2,data=dat, type="scatterChart")
r1$show('inline')
```

Right now, it displays the values on each point. I would like to also include "Gene_Name" along with the values. Any help will be appreciated as I have a presentation tomorrow. Thanks.

  • Welcome to SO! The `rCharts` package is not on CRAN. You are much more likely to receive help if you provide [all code necessary to run your example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Cheers. – Henrik Sep 29 '13 at 16:47

1 Answers1

3

Here is how to do it with rCharts. The key is to use the chart method and pass a javascript function to tooltipContent. It accepts four arguments, of which we will be using the e argument that provides access to the actual data points. So e.point.Gene_Name accesses the Gene_Name for each point. You can view a demo of this chart on rcharts viewer

dat<- data.frame(
  Gene_Name= LETTERS[1:15], 
  Value1 = sample(-8:20, 15, replace = TRUE),
  Value2 = sample(-6:10, 15, replace = TRUE)
)

library(rCharts)
r1<- nPlot(Value1~Value2,data=dat, type="scatterChart")
r1$chart(tooltipContent = "#! function(key, x, y, e){
  return '<b>Gene Name</b>: ' + e.point.Gene_Name
} !#")
r1

NOTE. You need the #! and !# tags to indicate to rCharts that the value is a JS literal. This will ensure that it passes it as a JS literal and not as a string while converting the payload to json.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • Brilliant! I could have never thought about this. Thank you very much. – user2828616 Sep 30 '13 at 03:19
  • The trouble is that a lot of options in nvd3 are not well documented. So you have to dig deeper into the source code. Added to that rCharts is also not documented, which makes things even harder. Hope to remedy it soon. – Ramnath Sep 30 '13 at 13:57
  • Once again thank you for the "slidify" package. My talk went really well. An unrelated question about publishing the html page. I know RPubs exist but doesn't want to make it public. Yesterday, I had some trouble in opening the html page as it seems to require net connection. Also, if I can't use my own laptop, the external images that I attached won't appear even if I copy the entire folder to another laptop. Only the figures where I used r codes will show on the slides. – user2828616 Oct 01 '13 at 13:23
  • You can control stuff using `mode`. Default is `standalone` in which case all images are embedded in the html, and js/css assets are served from an online repository. Setting it to `selfcontained` links everything from your slide folder, so you should be able to copy your slide folder to another computer and present even when offline. – Ramnath Oct 01 '13 at 14:11
  • Thanks. I checked the mode. It was already in the "selfcontained". – user2828616 Oct 01 '13 at 14:27
  • In that case, you should be able to copy your slide folder to any computer and present. To publish to RPubs, you need to switch mode to standalone, and then slidify again. – Ramnath Oct 01 '13 at 14:57