【问题标题】:ggplot2 hell with rpy2-2.0.7 + python 2.6 + r 2.11 (windows 7)ggplot2 地狱与 rpy2-2.0.7 + python 2.6 + r 2.11 (windows 7)
【发布时间】:2011-03-31 17:24:40
【问题描述】:

我正在使用 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: Error: 当设置 美学时,它们可能只取一个值。问题:填充,x

【问题讨论】:

  • 你能在 R 中创建 ggplot2 图,而不需要周围的所有 python 杂物吗?从 R 代码开始可能会更容易,然后尝试将其转换为 RPy2。将 R 封装在 RPy2 调用中会产生一个额外的调试层。
  • 使用ggsavepng() + dev.off()。您可能还需要明确地print() 该情节。
  • 或者只是重写R中的Python部分;-)
  • 感谢您的建议,我会试一试。哈哈,现在我正在这样做-> 将内容直接移植到 R 进行分析。不幸的是,我“需要”python,因为这是我正在使用 pylons 开发的网络应用程序。我可能会做的是摆脱大部分 python 并直接从 r 中提取我的 mssql rdms 中的数据。
  • 它可能会帮助您正确设置 ggplot2 代码(看起来主要是您的问题)。

标签: python r device ggplot2 rpy2


【解决方案1】:

我仅通过 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' 并将 'print "error in process_revents:ignore"' 替换为 'warn("error in process_revents:ignore" )'。这样,我每次会话只能看到一次警告。)

【讨论】:

  • 很酷的解决方案,乔恩。为了可靠性起见,我决定不在多个 api 之上构建我的应用程序 :) 但这绝对有效!谢谢,
【解决方案2】:

你必须在关闭它之前使用 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']()

【讨论】:

    【解决方案3】:

    为了在您必须绘制更复杂的图时稍微容易一些:

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

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 1970-01-01
      • 2016-05-11
      • 2012-03-21
      • 2011-11-23
      • 2011-07-01
      • 2013-11-23
      • 2020-02-11
      • 2014-07-19
      相关资源
      最近更新 更多