我正在使用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['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

有帮助吗?

解决方案

我仅通过纳撒尼尔·史密斯(Nathaniel Smith)的辉煌小模块使用RPY2 rnumpy (请参阅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_rnumpy模块的“ 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添加“从警告中添加了“导入警告”,然后替换'pronse_revents中的错误:忽略:with'wall'wall'wall'wall'wall'wall wall'wall wall(“ process_revents in crocess_revents:nivered new of bighored of bight of bight”)')'。这样,我每次会话只看到一次警告。)

其他提示

在关闭它之前,您必须参与dev(),这意味着您必须在抛售dev.off()之前打印()(如上面的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']()

当您必须绘制更复杂的情节时,使它变得更加容易:

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()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top