【发布时间】:2016-05-11 04:40:22
【问题描述】:
我正在尝试在 Python 中使用 R,我发现 Rpy2 非常有趣。它功能强大且使用起来并不难,但是即使我阅读了文档并寻找了类似的问题,我也无法使用 ggplot2 库解决我的问题。
基本上我有一个包含 2 列、11 行且没有标题的数据集,我想使用 Python 中的这个 R 代码做一个散点图:
ggplot(dataset,aes(dataset$V1, dataset$V2))+geom_point()+scale_color_gradient(low="yellow",high="red")+geom_smooth(method='auto')+labs(title = "Features distribution on Scaffolds", x='Scaffolds Length', y='Number of Features')
我已经在 R 中测试了这段代码(在 read.table 我的文件之后)并且它可以工作。现在,这是我的 python 脚本:
import math, datetime
import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.lib.ggplot2 as ggplot2
r = robjects.r
df = r("read.table('file_name.txt',sep='\t', header=F)")
gp = ggplot2.ggplot(df, ggplot2.aes(df[0], df[1])) + ggplot2.geom_point() + ggplot2.scale_color_gradient(low="yellow",high="red") + ggplot2.geom_smooth(method='auto') + ggplot2.labs(title = "Features distribution on Scaffolds", x='Scaffolds Length', y='Number of Features')
gp.plot()
如果我运行这个 Python 代码,它会给我两个错误。第一个是:
gp = ggplot2.ggplot(df, ggplot2.aes(df[0], df[1]))
TypeError: new() takes exactly 1 argument (3 given)
第二个是:
AttributeError: 'module' object has no attribute 'scale_color_gradient'
谁能帮我理解我错在哪里?
【问题讨论】:
标签: r python-2.7 ggplot2 rpy2