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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With