5

Is it possible to use rpy2 (calling ggplot2) with IPython notebooks, and then save them (and share on NBViewer like other notebooks http://nbviewer.ipython.org/)? Is there any challenge in having the rpy2 ggplots appear in the notebook and/or interactively? It would be helpful if someone could provide an example session and its output of making a ggplot2 figure within a notebook using rpy2 in IPython.

2 Answers2

4

This was written without looking the code in rmagic. They have have a more clever way to do it (I have 11 lines of code).

import uuid
from rpy2.robjects.packages import importr 
from IPython.core.display import Image

grdevices = importr('grDevices')
def ggplot_notebook(gg, width = 800, height = 600):
    fn = '{uuid}.png'.format(uuid = uuid.uuid4())
    grdevices.png(fn, width = width, height = height)
    gg.plot()
    grdevices.dev_off()
    return Image(filename=fn)

To try it:

from rpy2.robjects.lib import ggplot2
from rpy2.robjects import Formula
datasets = importr('datasets')
mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']
p = ggplot2.ggplot(mtcars) + \
    ggplot2.aes_string(x='mpg', y='cyl') + \
    ggplot2.geom_point() + \
    ggplot2.geom_smooth() + \
    ggplot2.facet_wrap(Formula('~ am'))

ggplot_notebook(p, height=300)
lgautier
  • 11,363
  • 29
  • 42
  • this code is the first time I was able to load and use datasetsfrom rpy2. The datasets from this page did not work:http://rpy.sourceforge.net/rpy2/doc-2.2/html/graphics.html#package-ggplot2 – zach Mar 20 '13 at 21:49
  • @zach: R changed the way it is internally dealing with datasets, and rpy2 was adapted to accommodate those changes (although the immediate result is not the most friendly interface). I cannot exclude that there were places in the documentation were this was not updated, but here the issue might be that you are looking at the documentation for rpy2-2.2.x while probably using rpy2-2.3.x. In any case, the best chance to see something fixed is to report the problem (email, project's page, etc...). – lgautier Mar 20 '13 at 22:26
  • Thanks. Sometimes its hard to even know what the problem is..... But I will do a better job of reporting. Thanks for your great work. – zach Mar 20 '13 at 22:42
1

It's possible with the rmagic extension, which uses rpy2. You seem to need to print() the figure to show it, though. Here's an example session: http://nbviewer.ipython.org/5029692

If you prefer to use rpy2 directly, it must be possible. Have a look at the rpy2 documentation for ggplot2. To get it into the notebook, you can draw to a PNG/SVG device, then read it from the Python side (this is what rmagic does).

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • I don't want to use `rmagic` I want to use rpy2 directly, but I don't see an example of a notebook with rpy2 on the page you linked to from the docs... not sure how to draw to a device and then read it back in? Is it not automated? –  Feb 25 '13 at 15:47
  • 2
    rmagic is what automates using a png device and reading the output. If you prefer to use rpy2 directly, you'll have to do that yourself. You can look at [the source code for rmagic](https://github.com/ipython/ipython/blob/master/IPython/extensions/rmagic.py). – Thomas K Feb 26 '13 at 13:03
  • That sounds like a major untaking... not worth it but it means ipython notebooks are basically not useful for people using ggplot/rpy2 instead of matplotlib –  Feb 27 '13 at 06:48
  • It's not a particularly complex endeavour - probably <10 lines of extra code. And again, it's already written as part of rmagic, which is just a wrapper around rpy2. – Thomas K Feb 27 '13 at 18:19