<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/ggplot2-style-plotting-in-python/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/"> Environmental Science and Data Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R is my language of choice for data science but a good data scientist should have some knowledge of all of the great tools available to them. Recently, I have been gleefully using Python for machine learning problems (specifically <a target="_blank" rel="nofollow" href="http://pandas.pydata.org/">pandas</a> and the wonderful <a target="_blank" rel="nofollow" href="http://scikit-learn.org/stable/">scikit-learn</a>). However, for all its greatness, I couldn’t help but feel it lacks a bit in the data visualisation department. Don’t get me wrong, <a target="_blank" rel="nofollow" href="https://matplotlib.org/">matplotlib</a> can be used to produce some very nice visualisations but I think the code is a bit messy and quite unintuitive when compared to Hadley Wickham’s <a target="_blank" rel="nofollow" href="http://ggplot2.org/">ggplot2</a>.</p>
<p>I’m a huge fan of the ggplot2 package and was delighted to discover that there has been an attempt to replicate its style in Python via the <a target="_blank" rel="nofollow" href="http://ggplot.yhathq.com/">ggplot</a> package. I wanted to compare the two packages and see just how well ggplot matches up to ggplot2. Both packages contain built-in datasets and I will use the mtcars data to build a series of plots to see how they compare, both visually and syntactically.</p>
<p>Here we go….</p>
<h2>Scatterplots</h2>
<p>Scatterplots are great for bivariate profiling and revealing relationships between variables.  A simple scatterplot using ggplot2 in R:</p>
<pre>ggplot(mtcars , aes(x = hp , y = mpg)) +
  geom_point()

</pre>
<p><img sizes="(max-width: 722px) 100vw, 722px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot.png?w=456" data-orig-size="722,316" data-image-title="Rplot" class="wp-image-1468" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="1468" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot-6/" alt="Rplot" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot.png?w=456?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot.png?w=456"></p>
<p>The same scatterplot using ggplot in Python:</p>
<pre>ggplot(mtcars , aes(x = 'hp' , y = 'mpg')) +\
    geom_point() 

<img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1.png?w=456" data-orig-size="1100,800" data-image-title="figure_1" data-comments-opened="1" class="wp-image-1480" data-recalc-dims="1" data-attachment-id="1480" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1/" alt="figure_1" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1.png?w=456"></pre>
<p>Not much of a difference there. The syntax is also very similar but in Python’s ggplot, there is a \ after each + when adding a new layer to a plot. When mapping variables to the xy coordinates, the use of inverted commas is also required in ggplot.</p>
<h3></h3>
<h3>Boxplots</h3>
<p>Boxplots are very nice for visualising discrete variables and the distributions of variables across them. In R’s ggplot2, I discretise the cyl variable with the factor() function to create a boxplot showing the distributions of mpg across each number of cylinders category (4, 6 and 8).</p>
<pre>ggplot(mtcars , aes(x = factor(cyl) , y = mpg)) +
  geom_boxplot()


<img sizes="(max-width: 722px) 100vw, 722px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot01.png?w=456" data-orig-size="722,316" data-image-title="Rplot01" data-comments-opened="1" class="wp-image-1488" data-recalc-dims="1" data-attachment-id="1488" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot01-5/" alt="Rplot01" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot01.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot01.png?w=456?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot01.png?w=456"></pre>
<p> </p>
<p>In Python, we need to discretise the cyl variable with the pandas.factorize() function before plotting with ggplot. The ordering is different in the Python plot output but reordering may be possible as it is in R. Also, note that the number of cylinders have been assigned dummy variables where 0 = 6 cylinders, 1 = 4 cylinders, and 2 = 8 cylinders.</p>
<div>
<pre>mtcars['cyl'] = pd.factorize(mtcars.cyl)[0]</pre>
<pre>ggplot(mtcars , aes(x = 'cyl' , y = 'mpg')) +\
  geom_boxplot()
<img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1-1.png?w=456" data-orig-size="1100,800" data-image-title="figure_1-1" data-comments-opened="1" class="wp-image-1496" data-recalc-dims="1" data-attachment-id="1496" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1-1/" alt="figure_1-1" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1-1.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1-1.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1-1.png?w=456">
</pre>
</div>
<h3>Histograms</h3>
<p>A fundamental tool for univariate profiling, histograms show the frequency distribution of a variable. In R’s ggplot2, I plot the distribution of mpg across the mtcars data and add a few more components such as margin outlines and red fill while bins are set to ten and x axis tick labels are modified.</p>
<pre>ggplot(mtcars , aes(x = mpg)) +
  geom_histogram(colour = &quot;black&quot; , fill = &quot;red&quot; , bins = 10) +
  scale_x_continuous(breaks = seq(0 , 40, 5))

</pre>
<p><img sizes="(max-width: 722px) 100vw, 722px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot02.png?w=456" data-orig-size="722,316" data-image-title="Rplot02" data-comments-opened="1" class="wp-image-1520" data-recalc-dims="1" data-attachment-id="1520" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot02-6/" alt="Rplot02" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot02.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot02.png?w=456?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot02.png?w=456"></p>
<p>With Python’s ggplot, the histogram is not as tidy. I couldn’t find a way to colour the margins black but there may be a way around this? The shape of the distribution looks a little different as well despite bins also being set to ten but this is just down to how the factoring is carried out in each language; the information within the plots is the same.</p>
<div>
<pre>ggplot(mtcars , aes(x = 'mpg')) +\
  geom_histogram(fill = 'red' , bins = 10)

<img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1-2.png?w=456" data-orig-size="1100,800" data-image-title="figure_1-2" data-comments-opened="1" class="wp-image-1525" data-recalc-dims="1" data-attachment-id="1525" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1-2/" alt="figure_1-2" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1-2.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1-2.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1-2.png?w=456">


</pre>
</div>
<h3>Facet Plots</h3>
<p>The facet wrapping function in ggplot2 can create fantastic visualisations when using larger datasets. A simple example is given in both implementations.</p>
<p>In R’s ggplot2, quarter mile time (qsec) is plotted against horsepower (hp) for each number of cylinders category. The facet wrapping splits the data into the specified discrete variable, in this case cyl, and plots the qsec/cyl relationship for each one.</p>
<pre>ggplot(mtcars , aes(x = hp , y = qsec)) +
 geom_point() +
 facet_wrap(~factor(cyl))

</pre>
<p><img sizes="(max-width: 722px) 100vw, 722px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot03.png?w=456" data-orig-size="722,316" data-image-title="Rplot03" data-comments-opened="1" class="wp-image-1537" data-recalc-dims="1" data-attachment-id="1537" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot03-3/" alt="Rplot03" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot03.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot03.png?w=456?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot03.png?w=456"></p>
<p>And in Python’s ggplot (note the same dummy variables for cyl are used), a similar ouput is seen. The slight difference is the absence of the grey border along the top of each plot in ggplot.</p>
<div>
<pre>ggplot(mtcars , aes(x = 'hp' , y = 'qsec')) +\
  geom_point() +\
  facet_wrap(~'cyl')</pre>
</div>
<div><img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1-3.png?w=456" data-orig-size="1100,800" data-image-title="figure_1-3" data-comments-opened="1" class="wp-image-1542" data-recalc-dims="1" data-attachment-id="1542" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1-3/" alt="figure_1-3" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1-3.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1-3.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1-3.png?w=456"></div>
<div></div>
<div></div>
<h3><strong>Making Things a Little Fancier</strong></h3>
<p>The previous examples are very simple, but fundamental, plots in data science. With ggplot2 in R, one can be highly creative with their data visualisations by representing categories by colour, facet wrapping, etc. to create plots which hold a lot of information. The following plots are quick examples of how one can be more creative using both packages.</p>
<div>In ggplot2, mpg is plotted against hp with each data instance now coloured according to number of cylinders.</div>
<pre>ggplot(mtcars , aes(x = hp , y = mpg , colour = factor(cyl))) +
  geom_point()</pre>
<div></div>
<div><img sizes="(max-width: 722px) 100vw, 722px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot04.png?w=456" data-orig-size="722,316" data-image-title="Rplot04" data-comments-opened="1" class="wp-image-1547" data-recalc-dims="1" data-attachment-id="1547" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot04-3/" alt="Rplot04" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot04.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot04.png?w=456?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot04.png?w=456"></div>
<div></div>
<div></div>
<div>In Python’s ggplot, a similar approach is used to colour each data instance by model of vehicle.</div>
<div>
<div>
<pre>ggplot(mtcars , aes(x = 'hp' , y = 'mpg' , color = 'name')) +\
  geom_point()</pre>
</div>
</div>
<div><img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1-4.png?w=456" data-orig-size="1428,800" data-image-title="figure_1-4" data-comments-opened="1" class="wp-image-1552" data-recalc-dims="1" data-attachment-id="1552" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1-4/" alt="figure_1-4" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1-4.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1-4.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1-4.png?w=456"></div>
<div></div>
<div></div>
<div>Using another built-in dataset (diamonds), we can look at the distribution of diamond prices by cut and colour is ironically used to represent diamond colour.</div>
<pre>ggplot(diamonds , aes(x = price , fill = color)) +
  geom_histogram(colour = &quot;black&quot;) +
  facet_wrap(~cut)</pre>
<div></div>
<div></div>
<div><img sizes="(max-width: 819px) 100vw, 819px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/rplot05.png?w=450&amp;h=359&amp;fit=819%2C359" data-orig-size="722,316" data-image-title="Rplot05" data-comments-opened="1" class="wp-image-1556" data-recalc-dims="1" data-attachment-id="1556" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/rplot05/" alt="Rplot05" data-medium-file="https://sw23993.files.wordpress.com/2017/05/rplot05.png?w=450&amp;h=359&amp;fit=819%2C359?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/rplot05.png?w=450&amp;h=359&amp;fit=819%2C359?w=722" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/rplot05.png?w=450&h=359&fit=819%2C359"></div>
<div></div>
<div>Python’s ggplot can produce a similar type of visualisation which in my opinion is less slick than the ggplot2 version.</div>
<div></div>
<div>
<div>
<pre>ggplot(diamonds , aes(x = 'price' , fill = 'color')) +\
  geom_histogram(colour = 'black') +\
  facet_wrap('cut')</pre>
</div>
</div>
<div><img sizes="(max-width: 739px) 100vw, 739px" data-image-description="" data-orig-file="https://sw23993.files.wordpress.com/2017/05/figure_1-5.png?w=456" data-orig-size="1100,800" data-image-title="figure_1-5" data-comments-opened="1" class="wp-image-1563" data-recalc-dims="1" data-attachment-id="1563" data-permalink="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/figure_1-5/" alt="figure_1-5" data-medium-file="https://sw23993.files.wordpress.com/2017/05/figure_1-5.png?w=456?w=300" data-large-file="https://sw23993.files.wordpress.com/2017/05/figure_1-5.png?w=456?w=739" data-image-meta="{" src="https://sw23993.files.wordpress.com/2017/05/figure_1-5.png?w=456"></div>
<div></div>
<h3> Conclusion</h3>
<p>That concludes this brief comparison of ggplot2 and ggplot. It is by no means exhaustive and I’m sure there are many ways of modifying Python plots in ggplot which I am unaware of for now. However, I am very grateful to the ggplot package creator <a target="_blank" rel="nofollow" href="https://github.com/yhat/ggpy">Greg Lamp</a> for allowing R fans to create ggplot2 style plots in Python and look forward to using the package in my Python endeavours.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sw23993.wordpress.com/1451/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/sw23993.wordpress.com/1451/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://sw23993.wordpress.com/2017/05/08/ggplot2-style-plotting-in-python/"> Environmental Science and Data Analytics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Elkz25NH92Q"></a></div>