其他分享
首页 > 其他分享> > 使用rpy2设置网格图选项时出现问题

使用rpy2设置网格图选项时出现问题

作者:互联网

我正在尝试使用numpy数组中的数据,使用rpy2和点阵来创建热图或颜色强度图.我正在使用python 2.6.2,R 2.10.1,rpy2 2.1.9,不确定是哪个版本的grid.我已经使其工作完美,除了我需要修改用于绘制相关变量(z)级别的色带的默认晶格设置.具体来说,我要使用灰度而不是品红色-青色默认渐变.这是在虚拟R中生成虚拟数据帧并创建灰度级水平图的代码:

library(lattice)

x <- rep(seq(1,10), each=10)
y <- rep(seq(1,10), 10)
z <- abs(rnorm(100))
z <- z/max(z)
df <- data.frame(x=x, y=y, z=z)

grayvector <- gray(seq(0,1,1/100))

foo <- levelplot(z ~ x * y, data=df, col.regions = grayvector)
print foo

使用rpy2,我无法设置col.regions参数.根据文档,rpy2应该转换any. _的函数参数中的字符.但是,这似乎不起作用,因为使用col_regions会导致参数被忽略.这是产生水平图的python代码,但没有灰度:

from __future__ import division
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
r = ro.r
lattice = importr("lattice")

grayvector = r.gray( r.seq(0, 1, 1/100))   
x = r.rep(r.seq(1,10), each=10)
y = r.rep(r.seq(1,10), 10)
z = r.abs(r.rnorm(100))

df = {'x': x, 'y' :y, 'z':z}
df = ro.DataFrame(foo)

formula = ro.Formula('z ~ x * y')
formula.getenvironment()['z'] = df.rx2('z')
formula.getenvironment()['y'] = df.rx2('y')
formula.getenvironment()['z'] = df.rx2('z')

foo = lattice.levelplot(formula, data=df, col_regions = grayvector)
print foo

有谁知道如何使用带的晶格函数参数.在rpy2中?

解决方法:

您需要手动指定参数映射:

from rpy2.robjects.functions import SignatureTranslatedFunction
lattice = importr("lattice")
lattice.levelplot = SignatureTranslatedFunction(lattice.levelplot,
                                                init_prm_translate={'col_regions': 'col.regions'})
foo = lattice.levelplot(formula, data=df, col_regions=grayvector)

还要检查一下:http://rpy.sourceforge.net/rpy2/doc-2.2/html/robjects_functions.html

It is important to understand that the
translation is done by inspecting the
signature of the R function, and that
not much can be guessed from the R
ellipsis ‘…’ whenever present.

标签:rpy2,lattice,python,r
来源: https://codeday.me/bug/20191105/1995576.html