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

1

7

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?

Bothersome answered 17/10, 2013 at 8:0 Comment(0)
M
18

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

Maimonides answered 17/10, 2013 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.