Search code examples
rgoogle-visualizationgooglevis

Adding column value labels to R googleVis gvisColumnChart?


Does anybody know how to add column value labels to a gvisColumnChart()?

There are so many options to go through, and I'm sure I'm not the first with this question. It would be really helpful for my project...

Edit: By value labels I mean something like this (numbers written on the bars themselves):

Chart with bar value labels

I'm using R 3.2.0 and googleVis 0.5.8

The answer for JS is here: https://developers.google.com/chart/interactive/docs/gallery/columnchart

Essentially creating a secondary view element and adding it to the plot:

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,
                   { calc: "stringify",
                     sourceColumn: 1,
                     type: "string",
                     role: "annotation" },
                   2]);

  var options = {...};
  var chart = new google.visualization.ColumnChart(document.getElementById("columnchart_values"));
  chart.draw(view, options);

I just don't quite know how to pass this extra element via R. Help?


Solution

  • I found a solution in R.

    Googlevis expects at an x-axis label column and at least one named data column. I duplicated the data column, resulting in a a three column dataset, in my case it's called chartset.

    Name the third column after the data column ("Percentage" in my case) and add ".annotation" to the name, thus "Percentage.annotation". The googlevis interpreter is smart enough to pick up on it and throw the third column in as an annotation.

      setnames(chartset, c("Answer","Percentage","Percentage.annotation"))
      p1 <- gvisColumnChart(chartset, xvar="Answer",  yvar=c('Percentage',                       
                            'Percentage.annotation'), options=list(title= ccTitle,
                             vAxes="[{minValue:0}]"))
      p1
    

    enter image description here