简体   繁体   中英

Why rpy2.robjects.lib.grid.viewport() return ListVector,but not viewport?

I am trying to plot several subgraphs in one graph using rpy2.robjects.lib.grid.viewport() , and this is the code:

from rpy2.robjects.lib import grid
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2
from rpy2 import robjects
dataf=robjects.DataFrame({'year':IntVector([2000,2001,2002,2001,2002]),
      'pop':IntVector([1.5,1.7,3.6,2.4,2.9]),'GDP':IntVector([4,5.3,6,7,8])})
grid.newpage()
lt=grid.layout(1,2)
vp=grid.viewport(layout=lt)
print(type(vp))
vp.push()
gp=ggplot2.ggplot(dataf)
vp = grid.viewport(**{'layout.pos.col':1, 'layout.pos.row': 1})
pp=gp+ggplot2.aes_string(x='pop',y='GDP')+ggplot2.geom_point()
pp.plot(vp=vp)
vp = grid.viewport(**{'layout.pos.col':2, 'layout.pos.row': 1})
pp=gp+ggplot2.aes_string(x='pop',y='GDP',col='factor(year)')+ggplot2.geom_point()
pp.plot(vp=vp)

But there in an error when executing "vp.push()". The error is as follows:

Traceback (most recent call last): File "F:\\pythonspace\\pythontoR2.py", line 15, in vp.push() AttributeError: 'ListVector' object has no attribute 'push'

I am using python3.4 and R3.2.1,rpy2 2.7.6 .

It seems like an oversight in rpy2.robjects.lib.grid .

Try replacing

vp=grid.viewport(layout=lt)

with

vp=grid.Viewport(grid.viewport(layout=lt))

(and may be file a bug report on the bug tracker for rpy2)

note: This filed as issue #350 , and a fix in the repos (will be included with rpy2 release 2.8.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM