ipython-notebook:打印rmagic情节作为svg

时间:2014-01-23 15:17:55

标签: r ipython-notebook rpy2

我使用以下代码片段在ipython notebook中使用ggplot绘制pandas数据帧。

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

结果是png格式的预期图。我希望有一个svg图像,但我无法弄清楚如何做到这一点。

1 个答案:

答案 0 :(得分:2)

在以前版本的 rpy2 中,我提出的解决方案无效。但是,对于rpy2-2.4.3,以下内容现在可以按预期工作:

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

设置R后,绘图按预期工作,显示的绘图将采用svg格式。

在python中将数据创建为pandas DataFrame:

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

最后进行策划:

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