Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nvd3 scatterPlot with rCharts in R: Vary size of points?

Tags:

r

nvd3.js

rcharts

I've been playing with rCharts and nvd3 for a bit of time now. Now I'm in a situation where I need a bubble chart, or at least a scatterplot where the size of the dots is dependent on a variable in the data. From this example, it seems possible. The example on scatter charts in rCharts is:

library(rCharts)
p1 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p1$xAxis(axisLabel = 'Weight')
p1

So I've tried setting size to, for example gears. But it doesn't change anything.

p2 <- nPlot(mpg ~ wt, group = 'cyl', size = 'gear', data = mtcars, type = 'scatterChart')
p2$xAxis(axisLabel = 'Weight')
p2

Is it possible?

like image 367
dynamo Avatar asked Oct 17 '13 08:10

dynamo


1 Answers

It is possible using the chart method, which allows you to specify size, color etc. The implementation is a little clunky right now, and requires you to pass a javascript function that returns the column specifying the size. The #! ... !# syntax is required to tell rCharts to treat the contents as a JS literal, and not convert it to a string while assembling the payload. The chart can be viewed here

library(rCharts)
p2 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p2$xAxis(axisLabel = 'Weight')
p2$chart(size = '#! function(d){return d.gear} !#')
p2

NVD3 Chart with Size

like image 64
Ramnath Avatar answered Sep 29 '22 14:09

Ramnath