Using R and Python together in a notebook
Installation¶
$ sudo apt-get install python-rpy2
$ conda install -c r r-essentials
$ conda install -c r r-rjson
$ pip install rpy2
Load the magic¶
In [1]:
%load_ext rpy2.ipython
Create a dataframe¶
Use Python and pandas to create a dataframe.
In [2]:
import pandas as pd
df_from_python = pd.DataFrame({'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]})
df_from_python
Out[2]:
Create plot in R¶
Import the dataframe df
with the -i
argument.
In [3]:
%%R -i df_from_python
require(ggplot2)
# Plot the DataFrame df
ggplot(data=df_from_python) + geom_point(aes(x=A, y=B, color=C)) + ggtitle('R scatter!')
Create a dataframe using R¶
Create a dataframe and export it using -o
.
In [4]:
%%R -o df_from_r
d <- c(2, 6, 6, 8, 4, 7, 5, 9, 3)
e <- c(3, 1, 4, 3, 6, 7, 10,11, 9)
f <- c(3, 1, 2, 3, 1, 2, 3, 1, 2)
df_from_r <- data.frame(d, e, f)
Now the variable is available for Python.
In [5]:
df_from_r
Out[5]:
Create plot with Python¶
In [6]:
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
matplotlib.style.use('ggplot')
plt.scatter(x=df_from_r['d'], y=df_from_r['e'], c=df_from_r['f'])
plt.title('Python scatter!')
plt.xlabel('A')
plt.ylabel('B')
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(10, 10)