Doughnut chart in R 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.
The guys at Google continue to update and enhance the Chart Tools API. One new recent feature is a pie chart with a hole, or as some call them: donut charts.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Thankfully the new functionality is being achieved through new options for the existing pie chart, which means that those new features are available in R via googleVis as well, without the need of writing new code.
Doughnut chart example
With the German election coming up soon, here is the composition of the current parliament.
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
dat <- data.frame(party=c("CDU", "FDP", "CSU", "SPD", | |
"The Left", "The Greens"), | |
members.of.parliament=c(193, 93, 44, | |
146, 76, 68)) | |
library(googleVis) | |
## Doughnut chart - a pie with a hole | |
doughnut <- gvisPieChart(dat, | |
options=list( | |
width=500, | |
height=500, | |
slices="{0: {offset: 0.2}, | |
1: {offset: 0.2}, | |
2: {offset: 0.2}}", | |
title='German parliament 2009 - 2013 | |
(Goverment: CDU/FDP/CSU)', | |
legend='none', | |
colors="['black','orange', 'blue', | |
'red', 'purple', 'green']", | |
pieSliceText='label', | |
pieHole=0.5), | |
chartid="doughnut") | |
plot(doughnut) |
Session Info
R version 3.0.1 (2013-05-16) 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 [7] base other attached packages: [1] googleVis_0.4.5 loaded via a namespace (and not attached): [1] RJSONIO_1.0-3 tools_3.0.1
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.