This was written without looking at the code in rmagic. They have a smarter way to do this (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:
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)
source
share