在jupyter笔记本中显示R ggplots

2024-04-26 12:52:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着运行一个简单的例子:https://www.datacamp.com/community/blog/jupyter-notebook-r#gs.OczVCjA

import warnings
warnings.filterwarnings('ignore')
# Load in the r magic

import rpy2.ipython

%reload_ext rpy2.ipython

# We need ggplot2

%R require(ggplot2)
%R library("ggplot2")
# Load in the pandas library
import pandas as pd 
# Make a pandas DataFrame
df = pd.DataFrame({'Alphabet': ['a', 'b', 'c', 'd','e', 'f', 'g', 'h','i'],
                   'A': [4, 3, 5, 2, 1, 7, 7, 5, 9],
                   'B': [0, 4, 3, 6, 7, 10,11, 9, 13],
                   'C': [1, 2, 3, 1, 2, 3, 1, 2, 3]})
# Take the name of input variable df and assign it to an R variable of the same name
%R -i df
# Plot the DataFrame df
ggplot(data=df) + geom_point(aes(x=A, y=B, color=C))

一开始我有一个名称错误“ggplot node defined”

然后,我将%R添加到最后一行,现在获得以下输出:

^{pr2}$

是否创建了绘图,以及如何像使用matplotlib一样在笔记本中显示它?在

注意:我使用conda install-crr-essentials命令在Jupyter中安装了R(通常包括ggplot?)如上述链接所述

事先非常感谢


Tags: thenameinimportdataframepandasdfipython
1条回答
网友
1楼 · 发布于 2024-04-26 12:52:47

好吧,我试过了。。。 这对我很有效:

%R print(ggplot(data=df) + geom_point(aes(x=A, y=B, color=C)))

您必须添加一个print()语句。不知道为什么。在

相关问题 更多 >