Python Rpy2&;ggplot2:LookupError';print.ggplot';
在不受任何关于R、Rpy2和ggplot2的已有知识的影响的情况下,我永远都不想从Python创建一个简单表的散点图 要设置此设置,我刚刚安装了:Python Rpy2&;ggplot2:LookupError';print.ggplot';,python,r,ggplot2,rpy2,scatter-plot,Python,R,Ggplot2,Rpy2,Scatter Plot,在不受任何关于R、Rpy2和ggplot2的已有知识的影响的情况下,我永远都不想从Python创建一个简单表的散点图 要设置此设置,我刚刚安装了: Ubuntu 11.10 64位 R版本2.14.2(来自R-cran镜像) ggplot2(通过R>install.packages('ggplot2')) rpy2-2.2.5(通过轻松安装) 接下来,我可以使用ggplot2从交互式R会话中绘制一些示例数据帧 但是,正如我在网上找到的一个示例中所看到的那样,当我仅尝试导入ggplot2时,会
- Ubuntu 11.10 64位
(来自R-cran镜像)R版本2.14.2
(通过ggplot2
)R>install.packages('ggplot2')
(通过rpy2-2.2.5
)轻松安装
ggplot2
时,会出现以下错误:
from rpy2.robjects.lib import ggplot2
File ".../rpy2/robjects/lib/ggplot2.py", line 23, in <module>
class GGPlot(robjects.RObject):
File ".../rpy2/robjects/lib/ggplot2.py", line 26, in GGPlot
_rprint = ggplot2_env['print.ggplot']
File ".../rpy2/robjects/environments.py", line 14, in __getitem__
res = super(Environment, self).__getitem__(item)
LookupError: 'print.ggplot' not found
虽然我无法帮助您修复您看到的导入错误,但这里有一个使用lattice的类似示例: 此外,标准的R
plot
函数通过使用factor
函数接受着色(您可以向行提供保留的列。示例:
plot(original.length, percentage.retained, type="p", col=factor(row.retained))
根据fucitol的回答,我改为使用默认plot和lattice实现plot。以下是两种实现:
from rpy2 import robjects
#Convert to R objects
original_lengths = robjects.IntVector(original_lengths)
percentages_retained = robjects.FloatVector(percentages_retained)
row_retained = robjects.StrVector(row_retained)
#Plot using standard plot
r = robjects.r
r.plot(x=percentages_retained,
y=original_lengths,
col=row_retained,
main='Title',
xlab='Percentage retained',
ylab='Original length',
sub='subtitle',
pch=18)
#Plot using lattice
from rpy2.robjects import Formula
from rpy2.robjects.packages import importr
lattice = importr('lattice')
formula = Formula('lengths ~ percentages')
formula.getenvironment()['lengths'] = original_lengths
formula.getenvironment()['percentages'] = percentages_retained
p = lattice.xyplot(formula,
col=row_retained,
main='Title',
xlab='Percentage retained',
ylab='Original length',
sub='subtitle',
pch=18)
rprint = robjects.globalenv.get("print")
rprint(p)
很遗憾,我无法使用ggplot2
,因为默认情况下,它会生成更好的图形,而且我认为使用数据帧更为明确。欢迎在这方面提供任何帮助!问题是由最新的ggplot2版本0.9.0引起的。此版本没有函数print.ggplot()可在ggplot2版本0.8.9中找到
我试图修补rpy2代码,使其与最新的ggplot2一起工作,但更改的范围似乎相当大
同时,只需将ggplot2版本降级到0.8.9即可。R包ggplot2中的更改破坏了rpy2层。
尝试使用bitbucket上rpy2代码的“默认”分支(rpy2-2.3.0-dev)的最新快照(我刚刚修复了此问题)
编辑:rpy2-2.3.0比计划晚了几个月。我刚刚推出了一个错误修复版本rpy2-2.2.6,应该可以解决这个问题。如果你没有任何使用R
的经验,但是使用python
,你可以使用numpy
或进行数据分析,matplotlib
进行绘图
下面是一个“这种感觉”的小例子:
pandas
还有一个实验性的rpy2接口。您好,感谢您解决了这个问题!导入再次生效,我正在考虑将我的代码转换为使用rpy2+ggplot2。
from rpy2 import robjects
#Convert to R objects
original_lengths = robjects.IntVector(original_lengths)
percentages_retained = robjects.FloatVector(percentages_retained)
row_retained = robjects.StrVector(row_retained)
#Plot using standard plot
r = robjects.r
r.plot(x=percentages_retained,
y=original_lengths,
col=row_retained,
main='Title',
xlab='Percentage retained',
ylab='Original length',
sub='subtitle',
pch=18)
#Plot using lattice
from rpy2.robjects import Formula
from rpy2.robjects.packages import importr
lattice = importr('lattice')
formula = Formula('lengths ~ percentages')
formula.getenvironment()['lengths'] = original_lengths
formula.getenvironment()['percentages'] = percentages_retained
p = lattice.xyplot(formula,
col=row_retained,
main='Title',
xlab='Percentage retained',
ylab='Original length',
sub='subtitle',
pch=18)
rprint = robjects.globalenv.get("print")
rprint(p)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'original_length': [1875, 1143, 960, 1302, 2016],
'row_retained': [False, False, False, False, True],
'percentage_retained': [11.0, 23.0, 44.0, 66.0, 87.0]})
fig, ax = plt.subplots()
ax.scatter(df.original_length, df.percentage_retained,
c=np.where(df.row_retained, 'green', 'red'),
s=np.random.randint(50, 500, 5)
)
true_value = df[df.row_retained]
ax.annotate('This one is True',
xy=(true_value.original_length, true_value.percentage_retained),
xytext=(0.1, 0.001), textcoords='figure fraction',
arrowprops=dict(arrowstyle="->"))
ax.grid()
ax.set_xlabel('Original Length')
ax.set_ylabel('Precentage Retained')
ax.margins(0.04)
plt.tight_layout()
plt.savefig('alternative.png')