Question

I use the following code fragment to plot a pandas dataframe with ggplot in ipython notebook.

%%R -i data
plot = ggplot(data) + geom_line(aes(x=x, y=y))
print(plot)

The result is the expected plot in png format. I would like to have an svg image instead, but I just cannot figure out how this can be done.

Was it helpful?

Solution

In previous versions of rpy2 my proposed solution did not work. However with rpy2-2.4.3 the following now works as expected:

%load_ext rpy2.ipython 
%R require(ggplot2)
%Rdevice svg

After setting up R, the plotting works as expected and the shown plot will be in svg format.

Create data as a pandas DataFrame in python:

import pandas as pd
data = pd.DataFrame({"x":[1,2,3], "y": [3,2,1]})

And finally do the plotting:

%%R -i data
plot = ggplot(data) + geom_line(aes(x=x, y=y))
print(plot)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top