Skip to content

Interactive plots in R?

1 message · Greg Snow

#
wandering
However,
R allows you quite a bit of flexibility and customization in plotting,
but you will 
have to decide if the look and feel matches with what you want.  I
address your
individual questions below.
The identify command will identify points (optionally label them on the
plot, then tell you which point(s) were selected).

The locator command will give you the location where the user clicked
on the plot, you could use this with the output from barplot to figure
out which bar was clicked on.  If you imediatly replot the graph with
the selected bar/point drawn differently then it will show the
selecection any way you want. 

The one thing I don't know how to make it do, is to differentiate
selection by clicking vs. 
ctrl-clicking like some packages allow.
This is not automatic, but easily implemented.  One of the easiest ways
is using the tcltk
package.  I have code (most of it modified from code at 
http://www.wiwi.uni-bielefeld.de/~wolf/) that will plot a 3d surface,
then pop up a window
with sliders, moving the sliders will change the viewing angles, light
angles, etc. of the plot.
You can also easily write code that has the user click on the plot,
then you redraw the plot 
zoomed or panned (R is quick enough that I don't notice the redraw).
There are 657 named colors, or you can specify the color you want using
rgb, or hsv values.
see the col option on the help page for "par", you can specify a
different color for each
line/point if you really want to.
simple, use something like: 
 plot(x,y, col=ifelse( z>M, 'red','black'), cex=ifelse( z>M, 1.5, 1) )

now points corresponding to variable z being greater than the value in
M will
be plotted 50% larger and red, others will be normal size and black.
Functions points, abline, lines, segments, arrows, polygon, text (and
maybe some others) will
plot additions to the current plot, use these to create bands, areas,
or whatever you want.
The defaults are usually pretty good, but you can specify anything you
want (including 
math markup like fractions, integral signs, greek letters, etc.  see
the help on plotmath).

You can suppress the initial axis, then use the axis command to
customize the axis, telling it
exactly where to draw tickmarks and how to label them (or let it figure
out something that looks nice).
see the tck option under the help for "par".  The command:

axis(side=2, tck=1, col='lightgrey', lty=4)
box()

will draw horizontal grid lines that are light grey and dashed, more
options are available to 
further customize this.
see the bmp and dev.copy functions (also can save to postscript, pdf,
jpeg, png, and other formats).
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help 
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

Greg Snow, Ph.D.
Statistical Data Center
greg.snow at ihc.com
(801) 408-8111