Setting axes limits 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.
I posted about the various googleVis axis options for base charts, such as line, bar and area charts earlier, but I somehow forgot to mention how to set the axes limits.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Unfortunately, there are no arguments such as
ylim
and xlim
. Instead, the Google Charts axes options are set via hAxes
and vAxes
, with h and v indicating the horizontal and vertical axis. More precisely, I have to set viewWindowMode : 'explicit'
and set the viewWindow
to the desired min
and max
values. Additionally, I have to wrap all of this in [{}]
brackets as those settings are sub-options of h/vAxes
. There are also options minValue
and maxValue
, but they only allow you to extend the axes ranges.Here is a minimal example, setting the y-axis limits from 0 to 10:
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
library(googleVis) | |
dat <- data.frame(x=LETTERS[1:10], | |
y=c(0, 4, -2, 2, 4, 3, 8, 15, 10, 4)) | |
area1 <- gvisAreaChart(xvar="x", yvar="y", data=dat, | |
options=list(vAxes="[{viewWindowMode:'explicit', | |
viewWindow:{min:0, max:10}}]", | |
width=500, height=400, | |
title="y-limits set from 0 to 10"), | |
chartid="area1ylim") | |
plot(area1) |
With more than one variable to plot I can use the
series
argument to decide which variables I want on the left and right axes and set the viewWindow
accordingly. Again, here is a minimal example:
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(1234) | |
dat <- data.frame(x=LETTERS[1:10], y_1=sample(15,10), y_2=sample(150,10)) | |
area2 <- gvisAreaChart(xvar="x", yvar=c("y_1", "y_2"), data=dat, | |
options=list( | |
series="[{targetAxisIndex:0}, | |
{targetAxisIndex:1}]", | |
vAxes="[{viewWindowMode:'explicit', | |
viewWindow:{min:0, max:10}}, | |
{viewWindowMode:'explicit', | |
viewWindow:{min:0, max:100}}]", | |
title="y-limits set individually", | |
width=500, height=400), | |
chartid="area2ylim") | |
plot(area2) |
Session Info
sessionInfo() 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 base other attached packages: [1] googleVis_0.4.3 loaded via a namespace (and not attached): [1] RJSONIO_1.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.