Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rpy2 & ggplot2: LookupError 'print.ggplot'

Unhindered by any pre-existing knowledge of R, Rpy2 and ggplot2 I would never the less like to create a scatterplot of a trivial table from Python.

To set this up I've just installed:

  • Ubuntu 11.10 64 bit
  • R version 2.14.2 (from r-cran mirror)
  • ggplot2 (through R> install.packages('ggplot2'))
  • rpy2-2.2.5 (through easy_install)

Following this I am able to plot some example dataframes from an interactive R session using ggplot2.

However, when I merely try to import ggplot2 as I've seen in an example I found online, I get the following error:

from rpy2.robjects.lib import ggplot2
  File ".../rpy2/robjects/lib/ggplot2.py", line 23, in <module>
    class GGPlot(robjects.RObject):
  File ".../rpy2/robjects/lib/ggplot2.py", line 26, in GGPlot
    _rprint = ggplot2_env['print.ggplot']
  File ".../rpy2/robjects/environments.py", line 14, in __getitem__
    res = super(Environment, self).__getitem__(item)
LookupError: 'print.ggplot' not found

Can anyone tell me what I am doing wrong? As I said the offending import comes from an online example, so it might well be that there is some other way I should be using gplot2 through rpy2.


For reference, and unrelated to the problem above, here's an example of the dataframe I would like to plot, once I get the import to work (should not be a problem looking at the examples). The idea is to create a scatter plot with the lengths on the x axis, the percentages on the Y axis, and the boolean is used to color the dots, whcih I would then like to save to a file (either image or pdf). Given that these requirements are very limited, alternative solutions are welcome as well.

     original.length row.retained percentage.retained
1               1875        FALSE                11.00
2               1143        FALSE                23.00
3                960        FALSE                44.00
4               1302        FALSE                66.00
5               2016        TRUE                 87.00
like image 475
Tim Avatar asked Jan 20 '26 13:01

Tim


2 Answers

There were changes in the R package ggplot2 that broke the rpy2 layer. Try with a recent (I just fixed this) snapshot of the "default" branch (rpy2-2.3.0-dev) for the rpy2 code on bitbucket.

Edit: rpy2-2.3.0 is a couple of months behind schedule. I just pushed a bugfix release rpy2-2.2.6 that should address the problem.

like image 152
lgautier Avatar answered Jan 23 '26 04:01

lgautier


Although I can't help you with a fix for the import error you're seeing, there is a similar example using lattice here: lattice with rpy2.

Also, the standard R plot function accepts coloring by using the factor function (which you can feed the row.retained column. Example:

plot(original.length, percentage.retained, type="p", col=factor(row.retained))
like image 27
Fukusahashi Avatar answered Jan 23 '26 03:01

Fukusahashi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!