ggplot2 地狱与 rpy2-2.0.7 + python 2.6 + r 2.11(Windows 7)

发布于 2024-09-15 11:20:34 字数 1690 浏览 3 评论 0原文

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

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

我真诚地感谢任何帮助;下面是一个(简单的)演示示例。非常感谢您的帮助!!! ~ Eric Butter


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: Error: No Layers inplot

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: When setting 美观,它们可能只采用一个值。问题:填充,x

i am using rpy2-2.0.7 (i need this to work with windows 7, and compiling the binaries for the newer rpy2 versions is a mess) to push a two-column dataframe into r, create a few layers in ggplot2, and output the image into a <.png>.

i have wasted countless hours fidgeting around with the syntax; i did manage to output the files i needed at one point, but (stupidly) did not notice and continued fidgeting around with my code ...

i would sincerely appreciate any help; below is a (trivial) example for demonstration. Thank you very much for your help!!! ~ Eric Butter


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

*The output is just a blank image (181 b).


here are a couple common errors R itself throws as I fiddle around in 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: Error: No layers in plot

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: When setting aesthetics, they may only take one value. Problems: fill,x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

折戟 2024-09-22 11:20:34

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

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“process_revents中的错误:忽略””替换为“warn(“process_revents中的错误:忽略”) )'。这样,我每次会话只会看到一次警告。)

I use rpy2 solely through Nathaniel Smith's brilliant little module called rnumpy (see the "API" link at the rnumpy home page). With this you can do:

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)

(I'm guessing a little about how you want the plot to look, but you get the idea.)

Another great convenience is entering "R mode" from Python with the ipy_rnumpy module. (See the "IPython integration" link at the rnumpy home page).

For complicated stuff, I usually prototype in R until I have the plotting commands worked out. Error reporting in rpy2 or rnumpy can get quite messy.

For instance, the result of an assignment (or other computation) is sometimes printed even when it should be invisible. This is annoying e.g. when assigning to large data frames. A quick workaround is to end the offending line with a trailing statement that evaluates to something short. For instance:

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

(To silence some recurrent warnings in rnumpy, I've edited rnumpy.py to add 'from warnings import warn' and replace 'print "error in process_revents: ignored"' with 'warn("error in process_revents: ignored")'. That way, I only see the warning once per session.)

一口甜 2024-09-22 11:20:34

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

You have to engage the dev() before you shut it off, which means that you have to print() (like JD guesses above) prior to throwing dev.off().

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']()
友谊不毕业 2024-09-22 11:20:34

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

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

To make it slightly more easy when you have to draw more complex plots:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文