In [1]:
# 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]})
In [2]:
%%R -i df
# Plot the DataFrame df
ggplot(data=df) + geom_point(aes(x=A, y=B, color=Alphabet, size=C))
In [3]:
# Create sample data by randomly selecting 1000 points from normal distribution
import pandas as pd
import numpy as np
data = np.random.randn(5000, 1)
df = pd.DataFrame(data, columns=["value"])
In [4]:
%%R -i df -w 800 -h 480 -u px
# Use magic extension and pass python dataframe created above
library(ggplot2)
ggplot(df) + geom_density(aes(x=value))
In [5]:
%R library(tidyverse)
Out[5]:
array(['dplyr', 'purrr', 'readr', 'tidyr', 'tibble', 'tidyverse',
       'ggplot2', 'tools', 'stats', 'graphics', 'grDevices', 'utils',
       'datasets', 'methods', 'base'], 
      dtype='|S9')
In [6]:
%%R
ggplot(data= mpg) + geom_point(mapping = aes(x = displ, y = hwy))
In [7]:
from ggplot import diamonds
import pandas as pd

df = pd.DataFrame(diamonds)

%R -i df
In [8]:
%%R 
ggplot(df, aes(x=carat, y=price, color=clarity)) + geom_point()
In [9]:
x = np.array([1, 2, 4, 6, 5, 8])
y = np.array([0, 1, 3, 2, 5, 7])
In [10]:
%%R -i x,y -o mycoef,xylm
xylm = lm(y~x)
mycoef = coef(xylm)
par(mfrow = c(2, 2))
plot(xylm)
In [11]:
import rpy2.robjects as ro
print(ro.r('summary(xylm)'))
print(ro.r('ls(xylm)'))
print(ro.r('residuals(xylm)'))

Call:

lm(formula = y ~ x)



Residuals:

    1     2     3     4     5     6 

-0.10  0.03  0.29 -2.45  1.42  0.81 



Coefficients:

            Estimate Std. Error t value Pr(>|t|)  

(Intercept)  -0.7700     1.2651  -0.609   0.5756  

x             0.8700     0.2565   3.392   0.0275 *

---

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1



Residual standard error: 1.481 on 4 degrees of freedom

Multiple R-squared:  0.7421,	Adjusted R-squared:  0.6776 

F-statistic: 11.51 on 1 and 4 DF,  p-value: 0.02747



['assign' 'call' 'coefficients' 'df.residual' 'effects' 'fitted.values'
 'model' 'qr' 'rank' 'residuals' 'terms' 'xlevels']
[-0.1   0.03  0.29 -2.45  1.42  0.81]