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

发布于 2024-10-13 04:40:33 字数 1267 浏览 6 评论 0原文

我正在尝试使用 numpy 数组中的数据、rpy2 和lattice 创建热图或颜色强度图。我正在使用 python 2.6.2、R 2.10.1、rpy2 2.1.9,不确定是哪个版本的lattice。我已经让它工作得很好,除了我需要修改用于绘制相关变量 (z) 水平的颜色渐变的默认晶格设置。具体来说,我想要灰度而不是洋红-青色默认渐变。下面是生成虚拟数据帧并在 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

使用 rpy2,我无法设置 col.regions 参数。根据文档,rpy2 应该将任何 . _ 的函数参数中的字符。然而,这似乎不起作用,因为使用 col_regions 会导致参数被忽略。这是生成 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

有谁知道如何使用带有 .在 rpy2 中?

I'm trying to create a heatmap or color-intensity plot using data from a numpy array, using rpy2 and lattice. I'm using python 2.6.2, R 2.10.1, rpy2 2.1.9, not sure which version of lattice. 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). 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:

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. According to the documentation, rpy2 is supposed to convert any . characters in function arguments to _ . This doesn't appear to be working, however, since using col_regions results in the argument being ignored. Here is the python code that produces the levelplot, but without grayscale:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2024-10-20 04:40:33

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

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

重要的是要了解
翻译是通过检查来完成的
R 函数的签名,并且
从 R 中猜不到太多
省略号“...”只要存在。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文