在Python和Rpy2中使用R:如何进行ggplot2? [英] Using R in Python with Rpy2: how to ggplot2?

查看:163
本文介绍了在Python和Rpy2中使用R:如何进行ggplot2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图在Python中使用R,但我发现Rpy2非常有趣.它功能强大并且使用起来并不难,但是即使我已经阅读了文档并寻找了类似的问题,我也无法使用ggplot2库解决我的问题.

Hi guys I'm trying to use R in Python and I found Rpy2 very interesting. It is powerful and not that difficult to use, however even if I have read the documentation and looked for a similar question, I wasn't able to solve my problem with the ggplot2 library.

基本上,我有一个包含2列,11行且没有标题的数据集,我想使用Python中的以下R代码绘制散点图:

Basically I have a dataset with 2 columns, 11 rows and no header and I would like to do a scatter plot using this R code from Python:

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脚本:

I have tested this code in R (after read.table my file) and it works. Now, this is my python script:

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代码,它将给我两个错误.第一个是:

If i run this Python code, it gives me two errors. The first is:

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'

有人可以帮我了解我错了吗?

Can someone help me to understand where I'm wrong please?

非常感谢您的光临!

马西米利亚诺(Massimiliano).

Massimiliano.

推荐答案

也许您需要将数据框列与散点图的颜色相关联 点,以便scale_colour_gradient可以与该列关联:

Perhaps you need to associate a dataframe column to the colour of the scatter points so that the scale_colour_gradient can be associated to that column:

import numpy as np
import pandas as pd
import rpy2.robjects.packages as packages
import rpy2.robjects.lib.ggplot2 as ggplot2
import rpy2.robjects as ro
R = ro.r
datasets = packages.importr('datasets')
mtcars = packages.data(datasets).fetch('mtcars')['mtcars']
gp = ggplot2.ggplot(mtcars)
pp = (gp 
      + ggplot2.aes_string(x='wt', y='mpg')
      + ggplot2.geom_point(ggplot2.aes_string(colour='qsec'))
      + ggplot2.scale_colour_gradient(low="yellow", high="red") 
      + ggplot2.geom_smooth(method='auto') 
      + ggplot2.labs(title="mtcars", x='wt', y='mpg'))

pp.plot()
R("dev.copy(png,'/tmp/out.png')")

错误

gp = ggplot2.ggplot(df, ggplot2.aes(df[0], df[1]))
TypeError: new() takes exactly 1 argument (3 given)

出现

的原因是ggplot2.ggplot仅接受1个参数,即数据帧:

occurred because ggplot2.ggplot takes only 1 argument, the dataframe:

gp = ggplot2.ggplot(df)

然后可以将美学映射添加到gp:

You can then add the aesthetics mapping to gp:

gp + ggplot2.aes_string(x='0', y='1')

,其中'0''1'df的列名.根据文档中示例,我在这里使用的是aes_string而不是aes.

where '0' and '1' are column names of df. Per the examples in the docs, I've used aes_string here instead of aes.

第二个错误

AttributeError: 'module' object has no attribute 'scale_color_gradient'

发生是因为ggplot2使用英国的颜色拼写:scale_colour_gradient:

occurred because ggplot2 uses the British spelling of color: scale_colour_gradient:

这篇关于在Python和Rpy2中使用R:如何进行ggplot2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆