【发布时间】:2019-09-15 22:11:56
【问题描述】:
我正在尝试在 Jupyter 笔记本中运行此代码
# Hide warnings if there are any
import warnings
warnings.filterwarnings('ignore')
# Load in the r magic
%load_ext rpy2.ipython
# We need ggplot2
%R require(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))
运行此代码后,我收到错误NameError: name 'ggplot' is not defined
事实上,当我运行%R require(ggplot2) 时,我得到一个空数组:array([0], dtype=int32)
更新
@James 评论后,如果我使用%R ggplot(data=df) + geom_point(aes(x=A, y=B, color=C)) 而不是ggplot(data=df) + geom_point(aes(x=A, y=B, color=C)),那么我会得到Error in ggplot(data = df) : could not find function "ggplot"
【问题讨论】:
-
对 ggplot 的访问发生在 R 内核中。试试
%R ggplot(data=df) + geom_point(aes(x=A, y=B, color=C)) -
@James 我试过了,现在我得到了
Error in ggplot(data = df) : could not find function "ggplot"。我会更新问题 -
你试过
%R ggplot2::ggplot(data=df) + ggplot2::geom_point(aes(x=A, y=B, color=C)),而不是加载包吗? -
@Jet 是的,但我得到了
Error in ggplot(data = df) : could not find function "ggplot"
标签: python r ggplot2 jupyter-notebook