1

假设我生成了一个像这样的 grob:

require(grid)
x = 1:10
y = rnorm(10)
plotvp = plotViewport(c(5, 5, 3, 3), name='plotvp')
datavp = dataViewport(x, y, name='datavp')
datapts = pointsGrob(x, y, pch=20, size=unit(1.3, 'mm'), name='datapts')
xaxis = xaxisGrob()
yaxis = yaxisGrob()
xlab = textGrob('X Label', y=unit(-3, 'lines'), name='xlab')
ylab = textGrob('Y Label', x=unit(-3, 'lines'), rot=90, name='ylab')
plotbox = rectGrob()
dataplot = gTree(children=gList(datapts,
                                xaxis, yaxis,
                                xlab, ylab,
                                plotbox),
                 vp=datavp, name='dataplot')
wholeplot = gTree(children=gList(dataplot),
                  vp=plotvp, name='wholeplot')

现在我想插入一组新的点:

x = 1:10
y = rnorm(10)

进入wholeplotgrob,我该怎么办?

根据@DWin 的回答,我做了以下事情:

x=1:10
y=rnorm(10)
datapts2 <- pointsGrob(x, y, pch=15, size=unit(1.3, 'mm'), name='datapts2')
wholeplot <- addGrob(wholeplot, datapts2)
grid.newpage()
grid.draw(wholeplot)
system('sleep 1')

datapts2 <- pointsGrob(x, y, pch=15, size=unit(1.3, 'mm'), name='datapts2')
wholeplot <- gTree(children=gList(dataplot, datapts2), vp=plotvp, name='wholeplot')
grid.newpage()
grid.draw(wholeplot)
system('sleep 1')

我没有看到任何效果。


经过反复试验,我意识到我需要为新数据点指定一个视口:

x=1:10
y=rnorm(10)
datapts2 <- pointsGrob(
                       x, y, default.units='native', 
                       pch=15, size=unit(1.3, 'mm'), name='datapts2', 
                       vp=datavp
                       )
wholeplot <- addGrob(wholeplot, datapts2)
grid.newpage()
grid.draw(wholeplot)

这成功了。

4

1 回答 1

2
datapts2 <- pointsGrob(x=1:10, y=rnorm(10), pch=20, 
                       size=unit(1.3, 'mm'), name='datapts2')
?gTree
wholeplot <- addGrob(wholeplot, datapts)

你也可以重建wholeplot我想的对象。第二个点-grob 不在“孩子”列表中。这样做对我来说看起来几乎一样:

wholeplot = gTree(children=gList(dataplot, datapts2),
                   vp=plotvp, name='wholeplot')
str(wholeplot)
于 2013-10-23T23:10:45.327 回答