Thursday, 26 July 2012

Interactive Plotting with Manipulate / Advanced Topics / Knowledge Base - RStudio Support

Oh gosh I can't believe how much time I wasted hardcoding values and tweak by editing the numbers and rerunning the scripts. 


RStudio includes a manipulate package that enables the addition of interactive capabilities to standard R plots. This is accomplished by binding plot inputs to custom controls rather than static hard-coded values.
http://support.rstudio.org/help/kb/advanced/interactive-plotting-with-manipulate

Basic Usage

The manipulate function accepts a plotting expression and a set of controls (e.g. slider, picker, or checkbox) which are used to dynamically change values within the expression. When a value is changed using its corresponding control the expression is automatically re-executed and the plot is redrawn.

For example, to create a plot that enables manipulation of a parameter using a slider control you could use syntax like this:

library(manipulate)  manipulate(plot(1:x), x = slider(1, 100))  

After this code is executed the plot is drawn using an initial value of 1 for x. A manipulator panel is also opened adjacent to the plot which contains a slider control used to change the value of x from 1 to 100.

Slider Control

The slider control enables manipulation of plot variables along a numeric range. For example:

manipulate(    plot(cars, xlim=c(0,x.max)),      x.max=slider(15,25))  

Results in this plot and manipulator:

Alt text

Slider controls also support custom labels and step increments.

No comments:

Post a Comment

Datanami, Woe be me