简体   繁体   English

使用rpy2设置晶格图选项的问题

[英]Problem setting lattice plot options using rpy2

I'm trying to create a heatmap or color-intensity plot using data from a numpy array, using rpy2 and lattice. 我正在尝试使用来自numpy数组的数据,使用rpy2和lattice来创建热图或颜色强度图。 I'm using python 2.6.2, R 2.10.1, rpy2 2.1.9, not sure which version of lattice. 我使用的是python 2.6.2,R 2.10.1,rpy2 2.1.9,不确定哪个版本的格子。 I've gotten it working perfectly, except that I need to modify the default lattice setting for the color ramp used to plot the levels of the relevant variable (z). 我已经完美地完成了它,除了我需要修改用于绘制相关变量(z)水平的颜色渐变的默认点阵设置。 Specifically, I want grayscale instead of the magenta-cyan default ramp. 具体来说,我想要灰度而不是品红色 - 青色默认斜坡。 Here is code to generate a dummy dataframe and create the grayscale levelplot in vanilla R: 下面是生成虚拟数据帧并在vanilla 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

With rpy2, I cannot set the col.regions argument. 使用rpy2,我无法设置col.regions参数。 According to the documentation, rpy2 is supposed to convert any . 根据文档,rpy2应该转换任何。 characters in function arguments to _ . _的函数参数中的字符。 This doesn't appear to be working, however, since using col_regions results in the argument being ignored. 但是,这似乎不起作用,因为使用col_regions会导致参数被忽略。 Here is the python code that produces the levelplot, but without grayscale: 这是生成levelplot的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

Does anyone know how to use lattice function arguments with a . 有谁知道如何使用格函数参数。 in them in rpy2? 在rpy2?

You need to specify the argument mapping manually: 您需要手动指定参数映射:

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)

And also check this: http://rpy.sourceforge.net/rpy2/doc-2.2/html/robjects_functions.html 另请查看: 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. 重要的是要理解翻译是通过检查R函数的签名来完成的,并且无论何时出现,都可以从R省略号“...”中猜到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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