简体   繁体   English

rpy2 plot问题

[英]rpy2 plot problem

I use rpy2-2.0.8, R-2.11.1, Python-2.6.2 on Windows XP.我在 Windows XP 上使用 rpy2-2.0.8、R-2.11.1、Python-2.6.2。

When I run this script, output image is filled with text message.当我运行这个脚本时,output 图像充满了文本消息。 I suppose this message is function definition of clusplot.我想这条消息是 clusplot 的 function 定义。

[test.py] [测试.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] [测试.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

But I use R console (not rpy2), No text overwapping problem.但我使用 R 控制台(不是 rpy2),没有文本重叠问题。

Does anyone help me?有人帮助我吗?

I have solved this problem by myself.我自己解决了这个问题。

R plot command completes the plot title automatically when it is not given. R plot 命令在未给出 plot 标题时自动完成。 R command with console and via rpy2 have different behavior for this complement.带有控制台和通过 rpy2 的 R 命令对此补码有不同的行为。 (I think it is a kind of bug for rpy2) (我认为这是 rpy2 的一种错误)

So set the title with "main" argument explicitly like this.所以像这样明确地用“main”参数设置标题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM