在多图窗口中绘制到特定的图中?

10
如果我使用par(mfrow=...)创建一个多图窗口,是否可以将数据发送到特定的绘图区域(例如“左下角的那个”),还是绘图总是按顺序进行?是否有适用于R的软件包可以完成此类操作?
对于那些感兴趣的人,在R中出现这个问题是因为R是单线程应用程序,并不理想用于实时可视化。我有多个实时数据流从外部来源进入R,这些数据是异步生成的(因此数据流的顺序不总是相同的)。这导致R每次更新时都会更改数据可视化图的顺序。
4个回答

11

你可以使用 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)

注意使用的重要性:par(bg="white") - Tal Galili

3
注意,这里建议使用split.screen()。它可能有效,但根据split.screen帮助文件的说法:“使用这些函数的推荐方法是在选择和绘制另一个屏幕之前,完全绘制出图形和所有附加项(即点和线)到基础图中。返回到屏幕上添加现有图形的行为是不可预测的,可能会导致问题,这些问题不容易被发现。”
在回答我的问题时,有一个更有用的解决方案,使用par(mfg)选项: 在R中更改多面板图中的绘图面板

3

请查看help(layout)。它允许您指定何时、何地以及以哪种大小显示。

绘制后,我认为您不能仅重新部分地重新绘制。但是,您可以使用dev.set()等来在不同的“绘图设备”(即窗口)之间切换;请参阅help(dev.list)


1
另一个选择是实现一个小的GUI,例如使用RGtk2RTclTk
我通常用这种方法来制作我想要实时更改的图表,效果非常好。
例如,使用RGtk2cairoDevice,您可以像这样做(我假设您有一个Glade界面):
# 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(....)

这有许多其他的优点,比如可以使用Glade界面设计师轻松地将图形定位在所需位置,并且通过特定按钮实现用户交互的可能性(例如,您可以拥有一个“暂停采集”按钮)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接