Sadly we don’t have any easy solution. Some naive ggplot base implementation is in PyPI however it’s not ggplot. We use Rpy2 in Python and it’s has problems in certain environments (for example in Kaggle can’t install packages.). Because of this, We use safer conda Environment for install Rpy2. For installation:
conda install rpy2
Interestingly little old version installed in my System (2.9.4). It’s okay not early unstable releases.
Okay we install it. Now we can use R in Python. Now we install ggplot2 in R. So we write this code (in Python) for this:
from rpy2.robjects.packages import importr utils = importr('utils')
if you want to select mirror before the installation you write this code before install_package directive:
mirror_number=12 #small positive integer <40 preferable utils.chooseCRANmirror(ind=mirror_number)
Okay after installation finished. We use library function for verify the installation:
import rpy2.robjects as robjects robjects.r("library(ggplot2)")
if we see StrVector with 9 elements like output it’s install successfully.
Okay We need to transform pandas dataframe to R’s equivalent. We do it automatically with Rpy2. Let’s write:
from rpy2.robjects importpandas2ri pandas2ri.activate()
Now we can use ggplot2 with pandas dataframe:
import rpy2.robjects.lib.ggplot2 as ggplot2
And for visualize plots we use this:
from rpy2.ipython.ggplot import image_png
.plot() function cause crash in my System. Maybe in your system works properly. Also sometimes random crashes is happen but not that often.
Originally published at http://anilkaynr.wordpress.com on June 8, 2020.