I'm just starting to learn how to use rpy2 with python. I'm able to make simple plots and such, but I've run into the problem that many options in R use ".". For example, here's an R call that works:
barplot(t, col=heat.colors(2), names.arg=c("pwn", "pwn2"))
where t is a matrix.
I want to use the same call in python, but it rejects the "." part of names.arg. My understanding was that in python you replace the "." with "_", so names_arg for example, but that is not working either. I know this is a basic problem so I hope someone has seen this and knows the fix. Thanks!
You can use a dictionary here for the named arguments (using **) as described in the docs, and call R directly for the functions. Also remember that RPy2 expects its own vector objects. Yes, it's a bit awkward, but on the plus side, you should be able to do anything in rpy2 you could do in R.
from rpy2 import robjects
color = robjects.r("heat.colors")()
names = robjects.StrVector(("pwn", "pwn2"))
robjects.r.barplot(t, col=color, **{"names.arg":names})
(Note that this is for rpy2 version 2.0.x; there are some changes in the unreleased 2.1 which I haven't had a chance to look at yet.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With