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