大家好我想在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')
Run Code Online (Sandbox Code Playgroud)
我已经在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()
Run Code Online (Sandbox Code Playgroud)
如果我运行这个Python代码,它会给我两个错误.首先是:
gp = ggplot2.ggplot(df, ggplot2.aes(df[0], df[1]))
TypeError: new() takes exactly 1 argument (3 given)
Run Code Online (Sandbox Code Playgroud)
第二个是:
AttributeError: 'module' object has no attribute …Run Code Online (Sandbox Code Playgroud)