Jake
Jake

Reputation: 15217

Plot to specific plot in multiple-plot window?

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.

Upvotes: 10

Views: 7617

Answers (4)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

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

Upvotes: 3

nico
nico

Reputation: 51640

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).

Upvotes: 1

rcs
rcs

Reputation: 68819

You could use split.screen():

par(bg = "white") # erase.screen() will appear not to work
                  # if the background color is transparent 
                  # (as it is by default on most devices).
split.screen(c(2,1)) # split display into two screens
split.screen(c(1,3), screen = 2) # now split the bottom half into 3
screen(1) # prepare screen 1 for output
plot(10:1)
screen(4) # prepare screen 4 for output
plot(10:1)

Upvotes: 11

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

Have a look at help(layout). This allows you to specify the what, where and in which sizes.

Once plotted, I don't think you re-plot just partially. But you you can use dev.set() et al to switch between different 'plot devices' (ie windows); see help(dev.list).

Upvotes: 3

Related Questions