rpy2情节问题

时间:2011-08-17 02:49:37

标签: python rpy2

我在Windows XP上使用rpy2-2.0.8,R-2.11.1,Python-2.6.2。

当我运行此脚本时,输出图像将填充文本消息。 我想这个消息是clusplot的函数定义。

[test.py]

#!/usr/bin/env python
# -*- mode: python -*- -*- coding: utf-8 -*-

import rpy2.robjects as ro
r = ro.r

# read from csv file
dataf = r('read.csv("test.csv", header=T, row.names="name")')

# k-means
r.library('cluster')
k = 2
cluster = r.kmeans(r.cmdscale(r.dist(dataf)), k)

# plot
r.jpeg('output.jpg')
r.clusplot(r.pam(dataf, k))
r('dev.off()')

[test.csv]

name,a,b,c,d,e,f,g,e,h,I,j,k
x1,1421,99.4,19.5,241.4,103.7,18.8,13.4,4.8,76.3,535.6,28.6,10.3
x2,1495,97.8,22.5,263.3,160.3,9.1,13.7,4.3,93.8,568,33.3,10.4
x3,2649,95.8,14.6,198.6,94.6,15.9,11.6,11.7,85,521.5,52.7,8.71
x4,3251,100.2,27.5,240.9,121,28,13.3,18.9,99.1,336.1,5.1,3.03
x5,2705,100.3,15.3,157.1,95.3,23.4,7.5,17,87.9,366.8,12.1,3.59
x6,3157,100.3,12.4,164,97.1,10.2,8.8,17.4,98.4,418.5,24.2,4.45
x7,2045,104.4,25.3,246.3,131,16.6,14,19.1,96.9,584.2,7.8,6.73
x8,2228,99.1,21.7,246.9,112.2,23.3,15.1,5.3,88.4,415.5,54.2,4.03
x9,2037,100.1,30,296.6,150.7,31.5,15.4,17.8,93.1,346.8,6.1,3.47
x10,2336,99.7,17.6,210.8,116.5,21.5,12.6,10.1,69.9,411,63.9,16.5
x11,1264,101.8,29.3,256.1,126.2,14.3,14.8,5.4,94,540.1,28.5,7.46
x12,1566,98.8,23.7,285.3,128.6,15.1,15.5,8.5,91.5,549.3,59.2,10.5
x13,2210,98.8,28.3,234.4,143.2,17.3,12.7,11.2,95.6,492.1,53.5,8.6

但我使用R控制台(不是rpy2),没有文字覆盖问题。

有人帮助我吗?

1 个答案:

答案 0 :(得分:1)

我自己解决了这个问题。

R plot命令在未给出时自动完成绘图标题。 带有控制台和通过rpy2的R命令对此补码具有不同的行为。 (我认为这是rpy2的一种bug)

所以用“main”参数设置标题就像这样。

# plot
r.jpeg('output.jpg')
r.clusplot(r.pam(dataf, k), main="result")
r('dev.off()')