Revo
Revo

Reputation: 87

Using R in Python with Rpy2: how to ggplot2?

I am 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.

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')

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()

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)

and the second is:

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

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

Upvotes: 5

Views: 5730

Answers (1)

unutbu
unutbu

Reputation: 880547

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')")

enter image description here


The error

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

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

gp = ggplot2.ggplot(df)

You can then add the aesthetics mapping to gp:

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

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


The second error

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

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

Upvotes: 13

Related Questions