How to Include R and ggplot in a Python Notebook | by Alan Jones | Towards Data Science

You are either an R person or a Python person, right? Unless, of course, you like some aspects of Python and others of R.
So why not mix and match? For example, R’s ggplot2 is a great visualization package, so wouldn’t it be good to be able to use that in a Python program?
We are going to see how we can achieve this — it’s quite easy — and then you can decide whether it is worth the effort.
I’m going to approach this from a Python user’s point of view, so I’ll assume that you already have Python and Jupyter installed on your computer. That means you need two more things: R and a Python package called rpy2. R needs to be installed because we are going to run R scripts (albeit in a Python context) and rpy2 is the bridge between the two languages.
So, of you go to the R website and download the version for your operating system and then follow the instructons to install R.
Done that? OK, next we are going to open the R console which will look something like this:
We are going to use the ggplot2 visualization package, so that needs to be installed. Type this in the console:
packages.install("tidyverse")
The tidyverse package includes ggplot2. (If you want to follow all of the code here you will also need to install the package ggalt.) Now you can close the R console.
Next we install the rpy2 package with pip:
pip install rpy2
We are now ready to write a Python Jupyter Notebook that can run cells written in R. Let’s open a Notebook and enter the following:
%load_ext rpy2.ipython
This loads the rpy2 package and enables the rmagic commands (as we will soon see). But first let’s import Pandas and create a dataframe.
import pandas as pd
And here is a dataframe that represents the monthly temperature that were recorded in London in 2018.
w = {  "Month":[1,2,3,4,5,6,7,8,9,10,11,12],  "Tmax":[9.7,6.7,9.8,15.5,20.8,24.2,28.3,24.5,20.9,16.5,12.2,10.7],  "Tmin":[3.8,0.6,3.0,7.9,9.8,13.1,16.4,14.5,11.0,8.5,5.8,5.2]  }weather=pd.DataFrame(w)
That gives us a dataframe like this:
Now we are going to plot a graph using ggplot2 in R. So we open a new cell and the first line that we need to enter is something this:
%%R -i weather -w 15 -h 10 --units cm
The %%R identifies the cell as R code and following this a a few parameters that determine how the cell will work. -i weather imports the previously created Python variable into the R cell, -w 15, -h 10, and — units cm, make any figure created 15 cm wide, and 10 cm high.
Here is a complete cell that inludes the %%R magic, imports the ggplot2 library and then plots a line graph from the weather dataframe.
%%R -i weather -w 15 -h 10 --units cm # import df from Python and set figure size 15 by 10cmlibrary("ggplot2")ggplot(weather,aes(Month, Tmax)) +     geom_line() +    ggtitle("Temperature")
The default plot is not ideal because the x ticks really ought to be from 1 to 12 and it could be a bit prettier. Let’s fix that.
%%R -i weather -w 15 -h 10 --units cm # import df from Python and set figure size 15 by 10cmlibrary("ggplot2")ggplot(weather) +     geom_line(aes(Month, Tmax),color="Red",size=1.25) +    scale_x_continuous(breaks=seq(1, 12, 1))+    scale_y_continuous(breaks=seq(-10, 40, 5))+    labs(y="Temperature", x="Month",          title="London Monthly Temperatures 2018") +    theme_light() +    theme(panel.grid.minor=element_blank())
There we see the neat way that we can build a chart, layer by layer, in R’s ggplot2.
But what if we want to plot both Tmax and Tminon the same chart. We need to transform the data so that the the two values are in the same column. We can do this in Python by opening a new cell and entering the following Python code.
weather2=pd.melt(weather, id_vars=['Month'],                  value_vars=['Tmax', 'Tmin'],var_name="Temp")
This creates a new dataframe weather2 that looks like this.
I’m sure that you can do this is R, too, but the idea here is to show how the two languages can be mixed.
Now we can plot a chart with ggplot2 that plots both values by creating a new R cell like this one.
%%R -i weather2 -w 15 -h 10 --units cm # import df from Python and set figure size 15 by 10cmlibrary("ggplot2")ggplot(weather2,aes(Month, value)) +     geom_line(aes(color=Temp),size=1.25)+    scale_x_continuous(breaks=seq(1, 12, 1))+    scale_y_continuous(breaks=seq(-10, 40, 5))+    labs(y="Temperature", x="Month",          title="London Monthly Temperatures 2018") +    theme_light() +    theme(panel.grid.minor=element_blank())
That gives us the following chart:
Now, at this point you might be thinking that this is all very well but I can do that with Plotnine without messing up my Python Notebook with a new language.
from plotnine import *(ggplot(weather2,aes("Month", "value")) +     geom_line(aes(color="Temp"),size=1.25)+    scale_x_continuous(breaks=range(1,13,1))+    scale_y_continuous(breaks=range(-10, 40, 5))+    labs(y="Temperature", x="Month",          title="London Monthly Temperatures 2018") +    theme_light() +    theme(panel_grid_minor=element_blank()))
And, of course that is true, as you can see from the code above. Plotnine is a fine package as I have described here but it is not a complete clone of ggplot as it is restrained in what it can achieve by Matplotlib, on which it is based.
For example, what if I wanted to create a dumbell chart of the monthly temperatures like this one.
This neat chart shows the range of temperatures for each month and, or course, with a bit of effort and ingenuity we could create that in Plotnine (or, indeed, in Matplotlib). But maybe it’s easier like this:
%%R -i weather -w 15 -h 10 --units cm # import df from Python and set figure size 15 by 10cmlibrary(ggplot2)library(ggalt)ggplot(weather, aes(y=Month, x=Tmin, xend=Tmax)) +  geom_dumbbell(size=1.25, color="Gray",                colour_x = "Blue",                 colour_xend = "Red",                size_x=4,                size_xend=4) +  labs(x="Temperature", y="Month",        title="London Monthly Temperatures 2018") +  scale_y_continuous(breaks=seq(1, 12, 1))+  scale_x_continuous(breaks=seq(-10, 40, 5))+  theme_light()
The library ggalt extends ggplot2 with additional plot types which makes plotting a dumbell chart quite simple.
As well as %%R there is also the magic command %R that lets you use individual lines of R coding in a Python cell but, frankly, I think mixing cells in different languages makes a Notebook messy enough, so I’ll stop at that.
So, R and Python in the same Notebook — good idea? Well, maybe. I like that I can use ggplot2 directly but then I also like Plotnine which allows me to use Python exclusively.
There is more to rpy2 than I have covered here. Take a look at their website for more details.
As always, thanks for reading. If you would like to know when I publish new articles, please consider signing up for an email alert here.
If you are not a Medium subscriber, how about signing up so you can read as many articles as you like for $5 a month. Sign up here and I’ll earn a small commision.