Annotation charts and histograms with googleVis

9 comments
After my posts on timeline, Sankey and calendar charts, this will be the last to introduce new chart types of the developer version of googleVis. Today I will give examples for the new annotation charts and histograms.

Annotation charts

Annotation charts have been part of the Google Chart tools for a long time and googleVis as well. However, in the past only a flash based version was available (gvisAnnotatedTimeLine in googleVis). With the new Google Charts Tools version also a HTML5 version was released. In googleVis it is integrated as gvisAnnotationChart. It works in exactly the same way as the Flash based version, so here is an example.



For the R code below to run you will require the developer version (≥ 0.5.0-4) of googleVis from GitHub and R ≥ 3.0.2.

Histograms

Histograms are a new chart type in Google Chart Tools API - not so in R. Still, it is nice to know that one can create those now as well.


For more information and installation instructions see the googleVis project site and Google documentation.

Session Info

R version 3.0.3 (2014-03-06)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base     

other attached packages:
[1] googleVis_0.5.0-4

loaded via a namespace (and not attached):
[1] RJSONIO_1.0-3 tools_3.0.3 

9 comments :

  1. Hi,

    Great post and package.

    Is it possible to use millisecond resolution with gvisAnnotatedTimeLine?



    Thanks

    ReplyDelete
  2. I am not sure, check out the Google documentation: https://developers.google.com/chart/interactive/docs/gallery/annotationchart#Data_Format

    ReplyDelete
  3. Thanks.

    Unfortunately, it doesn't seem to be possible. Although the following places the events at the correct location the x-axis has minimum 15 second resolution.

    Newdat <- data.frame(Date=as.POSIXct(c("2013-01-24 10:35:10.200", "2013-01-24 10:36:11.300", "2013-01-24 10:37:12.220","2013-01-24 10:38:13.400","2013-01-24 10:41:13.800")),
    Value=c(1,2,3,4,5),
    Label=c("Event1","Event2","Event3","Event4","Event5"))

    NewAA1 <- gvisAnnotatedTimeLine(Newdat, datevar ="Date",
    numvar ="Value",annotationvar="Label",
    options=list(displayAnnotations=TRUE,
    width=600, height=350))

    plot(NewAA1)

    ReplyDelete
  4. Thanks for your helpful post.
    I have created an html page with a gvisAnnotationChart (using rmarkdown::render) and tried to `include` it into a shiny app (using includeHTML). Unfortunately, the Annotation Chart does not display correctly. Are there any suggestions?

    ReplyDelete
  5. Hi George,
    You have to use the function renderGvis of the googleVis package to include the chart into a shiny application. The googleVis package vignette has more details on this, as well as the help file to renderGvis. Additionally, you find examples on my blog: http://lamages.blogspot.co.uk/2013/02/first-steps-of-using-googlevis-on-shiny.html

    I hope this helps.

    ReplyDelete
  6. Thank you, but I don't think that renderGvis can help, as I am not creating the chart in server.r. The chart is created in an r chunk of an .Rmd file. Then, this file is being rendered to html and the html page is included in a tabpanel using a command like this: tabPanel("My page", includeHTML("my_page.html")).

    ReplyDelete
  7. Ok, that is a different story. In this case ensure the chunk setting says results='asis' and for the plot simply say

    plot(plot(gvisAnnotationChart(Stock, Stock, datevar="Date", numvar="Value", idvar="Device", titlevar="Title", annotationvar="Annotation"), 'chart'), 'chart')

    The googleVis vignette has more details on knitr and Rmd files, or visit an old post of mine.

    ReplyDelete
  8. This is what I was doing (without the double 'Stock' of course). But the problem was that I had googleVis 0.5.2 Upgrading to 0.5.5 solved the problem. I tried to avoid upgrading to 0.5.5 because the other annotation chart (gvisAnnotatedTimeLine) is not working as expected anymore, but I guess I can use only the gvisAnnotationChart. Thank you.

    ReplyDelete
  9. This is how I do it. When I dbl-click the .html file the chart is shown correctly. But when I runApp, (or deployApp) the shiny app that `includes` this page, the result is like that http://imagizer.imageshack.us/v2/1024x768q90/537/Q6Fixh.png

    ReplyDelete