Search code examples
rgooglevis

How do I set the colorAxis of a googleVis bubble chart?


I try to adapt the GoogleVis bubble chart example with a continuous color scale for the bubbles based on the profit column:

library(googleVis)
Bubble <- gvisBubbleChart(Fruits, idvar="Fruit", 
                          xvar="Sales", yvar="Expenses",
                          colorvar="Profit", 
                          options=list(
                              ## custom color
                              ## colors = "['red', 'green', 'blue']",
                              ## custom color scale does not work
                              colorAxis="{colors: ['yellow', 'red']}",
                              hAxis='{minValue:75, maxValue:125}'))
plot(Bubble)

However, whatevery I try with the 'colorAxis' option it does not work, while I do follow the official documentation. It seems weird, because when I look at the source code of the plot I can see that the option is set:

// jsDrawChart
function drawChartBubbleChartID2b6add84971() {
var data = gvisDataBubbleChartID2b6add84971();
var options = {};
options["colorAxis"] = {colors: ['yellow', 'red']};
options["hAxis"] = {minValue:75, maxValue:125};
    var chart = new google.visualization.BubbleChart(
    document.getElementById('BubbleChartID2b6add84971')
    );
    chart.draw(data,options);   
}

What am I doing wrong? Thanks for any help.


Solution

  • UPDATE:

    After I added an issue on GitHub, the problem should be resolved for the current developer version on GitHub (devtools::install_github("mages/googleVis")). And indeed, it works:

    enter image description here


    The problem seems to be the data column "Profit":

    var datajson = [
                    ["Apples", 98, 78, "20"],
                    ["Oranges", 96, 81, "15"],
                    ["Bananas", 85, 76, "9"]
                   ];
    
    data.addColumn('string','Fruit');
    data.addColumn('number','Sales');
    data.addColumn('number','Expenses');
    data.addColumn('string','Profit');
    

    For reasons I do not see this column is defined as a string type column. When you change the relevant lines to

    var datajson = [
                        ["Apples", 98, 78, 20],
                        ["Oranges", 96, 81, 15],
                        ["Bananas", 85, 76, 9]
                       ];
    
    data.addColumn('number','Profit');
    

    a gradient color scale is applied to the bubbles.

    This might be a bug in the googleVis implementation since the Fruits data defines the Profit column to be numeric.