Annotation charts and histograms with googleVis
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Uncomment the next 3 lines to install the developer version of googleVis | |
# install.packages(c("devtools","RJSONIO", "knitr", "shiny", "httpuv")) | |
# library(devtools) | |
# install_github("mages/googleVis") | |
library(googleVis) | |
Stock | |
# Date Device Value Title Annotation | |
# 1 2008-01-01 Pencils 3000 <NA> <NA> | |
# 2 2008-01-02 Pencils 14045 <NA> <NA> | |
# 3 2008-01-03 Pencils 5502 <NA> <NA> | |
# 4 2008-01-04 Pencils 75284 <NA> <NA> | |
# 5 2008-01-05 Pencils 41476 Bought pencils Bought 200k pencils | |
# 6 2008-01-06 Pencils 333222 <NA> <NA> | |
# 7 2008-01-01 Pens 40645 <NA> <NA> | |
# 8 2008-01-02 Pens 20374 <NA> <NA> | |
# 9 2008-01-03 Pens 50766 <NA> <NA> | |
# 10 2008-01-04 Pens 14334 Out of stock Ran out of stock of pens at 4pm | |
# 11 2008-01-05 Pens 66467 <NA> <NA> | |
# 12 2008-01-06 Pens 39463 <NA> <NA> | |
plot( | |
gvisAnnotationChart(Stock, datevar="Date", | |
numvar="Value", idvar="Device", | |
titlevar="Title", annotationvar="Annotation", | |
options=list( | |
width=600, height=350, | |
fill=10, displayExactValues=TRUE, | |
colors="['#0000ff','#00ff00']"), | |
chartid="AnnotationChart" | |
) | |
) |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(123) | |
dat=data.frame(A=rpois(100, 20), | |
B=rpois(100, 5), | |
C=rpois(100, 50)) | |
plot( | |
gvisHistogram(dat, options=list( | |
legend="{ position: 'top', maxLines: 2 }", | |
colors="['#5C3292', '#1A8763', '#871B47']", | |
width=600), | |
chartid="Histogram") | |
) |
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
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.