5

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Revo
  • 87
  • 2
  • 10

1 Answers1

13

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:

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • I have changed my code following your tips and it works very well! Thank you very much for your help, hope I reach level 15 soon to up-vote your comment. Thank you again :) – Revo Feb 02 '16 at 17:48
  • The American spelling will be added shortly. Thanks for pointing that out @unutbu – lgautier Feb 02 '16 at 22:33
  • When I tried this in Colab, it just outputs IntVector with 1 elements. 5 instead of the plot. How to fix it? – WebComer Aug 04 '21 at 10:50