3

我正在使用 rpy2-2.0.7(我需要它才能与 Windows 7 一起使用,并且为较新的 rpy2 版本编译二进制文件是一团糟)将两列数据帧推入 r,在 ggplot2 中创建几层,然后输出将图像转换为 <.png>。

我已经浪费了无数个小时来摆弄语法;我确实设法输出了我需要的文件,但是(愚蠢地)没有注意到并继续摆弄我的代码......

我真诚地感谢任何帮助;下面是一个(简单的)演示示例。非常感谢您的帮助!!!〜埃里克黄油


import rpy2.robjects as rob
from rpy2.robjects import r
import rpy2.rlike.container as rlc
from array import array

r.library("grDevices")    # import r graphics package with rpy2
r.library("lattice")
r.library("ggplot2")
r.library("reshape")

picpath = 'foo.png' 

d1 = ["cat","dog","mouse"]
d2 = array('f',[1.0,2.0,3.0])

nums = rob.RVector(d2)
name = rob.StrVector(d1)

tl = rlc.TaggedList([nums, name], tags = ('nums', 'name'))
dataf = rob.RDataFrame(tl)

## r['png'](file=picpath, width=300, height=300)
## r['ggplot'](data=dataf)+r['aes_string'](x='nums')+r['geom_bar'](fill='name')+r['stat_bin'](binwidth=0.1)
r['ggplot'](data=dataf)
r['aes_string'](x='nums')
r['geom_bar'](fill='name')
r['stat_bin'](binwidth=0.1)
r['ggsave']()
## r['dev.off']()

*输出只是一个空白图像(181 b)。


这是我在 ggplot2 中摆弄时 R 本身抛出的几个常见错误:

r['png'](file=picpath, width=300, height=300)
r['ggplot']()
r['layer'](dataf, x=nums, fill=name, geom="bar")
r['geom_histogram']()
r['stat_bin'](binwidth=0.1)
r['ggsave'](file=picpath)
r['dev.off']()

*RRuntimeError:错误:绘图中没有图层

r['png'](file=picpath, width=300, height=300)
r['ggplot'](data=dataf)
r['aes'](geom="bar")
r['geom_bar'](x=nums, fill=name)
r['stat_bin'](binwidth=0.1)
r['ggsave'](file=picpath)
r['dev.off']()

*RRuntimeError: 错误:设置美学时,它们可能只取一个值。问题:填充,x

4

3 回答 3

3

我仅通过 Nathaniel Smith 的名为 rnumpy 的出色小模块使用rpy2(请参阅 rnumpy 主页上的“API”链接)。有了这个,你可以这样做:

from rnumpy import *

r.library("ggplot2")

picpath = 'foo.png' 
name = ["cat","dog","mouse"]
nums = [1.0,2.0,3.0]

r["dataf"] = r.data_frame(name=name, nums=nums)
r("p <- ggplot(dataf, aes(name, nums, fill=name)) + geom_bar(stat='identity')")
r.ggsave(picpath)

(我猜测您希望情节看起来如何,但您明白了。)

另一个极大的便利是使用 ipy_rnumpy 模块从 Python 进入“R 模式”。(请参阅 rnumpy 主页上的“IPython 集成”链接)。

对于复杂的东西,我通常在 R 中进行原型设计,直到我完成了绘图命令。rpy2 或 rnumpy 中的错误报告可能会变得非常混乱。

例如,有时会打印分配(或其他计算)的结果,即使它应该是不可见的。这很烦人,例如在分配给大型数据帧时。一个快速的解决方法是用一个评估为简短内容的尾随语句结束有问题的行。例如:

In [59] R> long <- 1:20
Out[59] R>
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20

In [60] R> long <- 1:100; 0
Out[60] R> [1] 0

(为了使 rnumpy 中的一些经常性警告静音,我编辑了 rnumpy.py 以添加 'from warnings import warn' 并用 'warn("error in process_revents:ignore")' 替换 'print "error in process_revents:ignore"'。这样,我每次会话只能看到一次警告。)

于 2010-11-26T14:58:04.320 回答
2

你必须在关闭它之前使用 dev(),这意味着你必须在抛出 dev.off() 之前 print()(就像上面的 JD 猜测一样)。

from rpy2 import robjects                          
r = robjects.r                                                                                    
r.library("ggplot2")
robjects.r('p = ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()') 
r.ggsave('/stackBar.jpeg') 
robjects.r('print(p)')
r['dev.off']()
于 2011-01-21T22:51:09.180 回答
1

当您必须绘制更复杂的图时,使其更容易:

from rpy2 import robjects
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2
r = robjects.r
grdevices = importr('grDevices')
p = r('''
  library(ggplot2)

  p <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
  p <- p + opts(title = "{0}")
    # add more R code if necessary e.g. p <- p + layer(..)
  p'''.format("stackbar")) 
  # you can use format to transfer variables into R
  # use var.r_repr() in case it involves a robject like a vector or data.frame
p.plot()
# grdevices.dev_off()
于 2011-02-17T10:58:05.120 回答