3

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?

2
  • 1
    "According to the documentation, rpy2 is supposed to convert any . characters in function arguments to _ .". If this is the case this is an oversight and it should be corrected. Can you point out where exactly in the documentation ? Commented Jan 26, 2011 at 1:15
  • @lgautier: I see something like that phrase here rpy.sourceforge.net/rpy2/doc-2.2/html/robjects_functions.html in the second paragraph (immediately after the first code example), but I believe tagshell is misinterpreting the semantic meaning of single dots as parts of argument names versus the semantic meaning of groups of 3 dots. Commented Oct 17, 2012 at 14:58

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Good to know because there are a lot of settings like this in lattice. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.