2017-05-15 32 views
2

我试图运行在这里发现了一个简单的例子:https://www.datacamp.com/community/blog/jupyter-notebook-r#gs.OczVCjA显示[R ggplots内嵌在jupyter笔记本

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)) 

起初,我不得不

我加入%R到NameError“定义ggplot节点”最后一行,现在得到以下的输出:

R object with classes: ('gg', 'ggplot') mapped to: 
<ListVector - Python:0x7fc6a73c1d88/R:0x3c4e768> 
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...] 
R object with classes: ('gg', 'ggplot') mapped to: 
<ListVector - Python:0x7fc6a73c1d88/R:0x3c4e768> 
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...] 
R object with classes: ('gg', 'ggplot') mapped to: 
<ListVector - Python:0x7fc6a73c1d88/R:0x3c4e768> 
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...] 
    scales: <class 'rpy2.robjects.environments.Environment'> 
    R object with classes: ('ScalesList', 'ggproto') mapped to: 
<Environment - Python:0x7fc6a7682808/R:0x215a968> 
    ... 
    data: <class 'rpy2.robjects.environments.Environment'> 
    R object with classes: ('FacetNull', 'Facet', 'ggproto') mapped to: 
<Environment - Python:0x7fc6a76829c8/R:0x281c138> 
    layers: <class 'rpy2.robjects.environments.Environment'> 
    R object with classes: ('environment',) mapped to: 
<Environment - Python:0x7fc6a7682548/R:0x1544d58> 
R object with classes: ('gg', 'ggplot') mapped to: 
<ListVector - Python:0x7fc6a73c1d88/R:0x3c4e768> 
[DataF..., ListV..., Envir..., ..., Envir..., Envir..., ListV...] 

是一场阴谋创建以及如何在笔记本内嵌显示它像一个将与matplotlib吗?

NB:我内Jupyter使用命令康达安装-CR 4 R 5 - 要领(?至极通常包括ggplot)如上述

在链接描述非常感谢您预先

回答

2

好安装R,我想现在... 这为我工作:

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

您必须添加一个print()声明。不知道为什么。

+1

谢谢,它的工作原理。我想知道为什么它需要在这里,将是很好的发现。 – user7188934