Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot to specific plot in multiple-plot window?

Tags:

plot

r

If I create a multi-plot window with par(mfrow=...), is it possible to send data to a specific plot (i.e. "the one in the lower left corner") or is the plotting always necessarily sequential? Is there a package for R that does something like this?

For those that are interested, this problem arises out of the fact that R is a single-threaded application and is not ideal for real-time visualization. I have multiple real-time data streams coming into R from an outside source that produces the data asynchronously (and therefore the data streams don't always come in the same order). This results in R flipping around the order of the data visualization plots every time it updates.

like image 634
Jake Avatar asked Jun 30 '10 16:06

Jake


2 Answers

Note that the suggested answer here is to use split.screen(). It may work, but according to the split.screen help file: "The recommended way to use these functions is to completely draw a plot and all additions (i.e. points and lines) to the base plot, prior to selecting and plotting on another screen. The behavior associated with returning to a screen to add to an existing plot is unpredictable and may result in problems that are not readily visible."

In an answer to my question, there is a more useful solution, using the par(mfg) option:

Change plot panel in multipanel plot in R

like image 183
Michael K. Borregaard Avatar answered Oct 09 '22 08:10

Michael K. Borregaard


Another option is that of implementing a little GUI e.g. with RGtk2 or RTclTk.

I generally do this for graphs that I want to change in realtime and it works great.

For instance, with RGtk2 and cairoDevice you could just do something like (I assume you have a Glade interface)

# Helper function to get a widget from the Glade interface
getWidget <- function(name)
 {
 return (interface$getWidget(name))
 }

interface <- gladeXMLNew("interface.glade", root="mainWindow")
# Our cairo devices (to draw graphics).
# plot1, plot2, and plot3 are GtkDrawingArea widgets 
asCairoDevice(getWidget("plot1"))
# dev.cur() will give the device number of the last device we created
# You'll use this to switch device when you draw in different plots
# Storing the device number is important because you may have other
# devices open from other unrelated plots 
# (so never assume they'll just start from 1 and be sequential!!!)
plot1.dev <- as.integer(dev.cur())
asCairoDevice(getWidget("plot2"))
plot2.dev <- as.integer(dev.cur())
asCairoDevice(getWidget("plot3"))
plot3.dev <- as.integer(dev.cur())

# To draw in a specific plot you just do
dev.set(plot2.dev)
plot(....)

This has many other advantages, like that of being able to positions the graphs easily where you want (using Glade Interface Designer) and having the possibility of user interaction through specific buttons (e.g. you may have a "pause acquisition" button).

like image 33
nico Avatar answered Oct 09 '22 09:10

nico