私はrpy2-2.0.7を使用しています(これはWindows 7で動作するために必要であり、新しいrpy2バージョンのバイナリをコンパイルするのは面倒です)2列のデータフレームを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自体がスローするいくつかの一般的なエラーがあります。
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:エラー:美学を設定するとき、それらは1つの値しかとらない場合があります。問題:fill、x