ggplot2地狱rpy22.0.7+python 2.6+r2.11(windows7)

2024-06-16 11:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用rpy2-2.0.7(我需要它来与windows7一起工作,并且为更新的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']()

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


下面是我在ggplot2中摆弄时R自己犯下的几个常见错误:

^{pr2}$

*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


Tags: nameimportpnglibrarybararrayfilerob
3条回答

我只通过NathanielSmith的名为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)

(我猜了一下你想让情节看起来怎么样,但你明白了。)

另一个非常方便的方法是从Python使用ipy_numpy模块进入“R模式”。(请参阅rnumpy主页上的“IPython集成”链接)。在

对于复杂的东西,我通常在R中建立原型,直到我完成了绘图命令。rpy2或rnumpy中的错误报告会变得相当混乱。在

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

^{pr2}$

(为了使rnumpy中一些反复出现的警告保持沉默,我编辑了rnumpy.py添加“from warnings import warn”并将“print”error in process_revents:ignored“”替换为“warn(“error in process_revents:ignored”)'。这样,每次会话我只看到一次警告。)

您必须在关闭dev()之前启动它,这意味着您必须在抛出之前print()开发关闭(). 在

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']()

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

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()

相关问题 更多 >