<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/putting-the-cat-in-scatterplot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.mango-solutions.com/blog/putting-the-cat-in-scatterplot"> Mango Solutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img sizes="100vw" src="https://i0.wp.com/www.mango-solutions.com/blog/images/3/5/4/b/c/354bcb0554869ee7e9ac73d30eef9c3046da2379-blog-image.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5>Clara Schartner, Data Scientist</h5>
<p>It will come as no surprise that cats and ggplot are among our favourite things here at Mango, luckily there is an easy way to combine both.</p>
<p>Using the function <code>annotation_custom</code> in the popular ggplot2 package it is possible to display images on a plot i.e. points of a scatterplot. This way data can be displayed in a more fun, creative way. </p>
<p>In keeping with the cat theme I have chosen a data set about cats and a cat icon based on Mango the cat. The MASS package provides a data set called <code>cats</code> which contains the body weight, heart weight and sex of adult cats.<br>
<img alt="" sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/142.putting-the-cat-in-scatterplot/scatterplot.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>library(MASS)
data(cats)
head(cats)
set.seed(1234)
cats &lt;- cats[sample(1:144, size = 40),]</code></pre>
<p>First a normal scatterplot is defined on which the images will be plotted later:</p>
<pre><code>library(ggplot2)
sCATter &lt;-ggplot(data = cats, aes(x = Bwt, y = Hwt)) +
  geom_point(size = 0, aes(group = Sex, colour = Sex)) +
  theme_classic() +
  xlab(&quot;Body weight&quot;) +
  ylab(&quot;Heart weight&quot;) +
  ggtitle(&quot;sCATterplot&quot;) +
  theme(plot.title = element_text(hjust = 0.5)) +
  # create a legend
  scale_color_manual(
    values = c(&quot;#999999&quot;, &quot;#b35900&quot; ),
    name = &quot;Cat&quot;,
    labels = c(&quot;Male cat&quot;, &quot;Female cat&quot;)
  ) +
  guides(colour = guide_legend(override.aes = list(size = 10)))</code></pre>
<p>Any png image can be used for the plot, however images with a transparent background are preferable.</p>
<pre><code>library(png)
library(grid)
mCat &lt;- readPNG(&quot;MaleCat.png&quot;)
feCat&lt;- readPNG(&quot;FemaleCat.png&quot;)</code></pre>
<p>In the last step the cats are iteratively plotted onto the plot using <code>annotation_custom</code>.</p>
<pre><code>for (i in 1:nrow(cats)) {
  # distinguishing the sex of the cat
  if (cats$Sex[i] == &quot;F&quot;) {
  image &lt;- feCat
  } else{
  image &lt;- mCat
  }
  sCATter = sCATter +
  annotation_custom(
    rasterGrob(image),
    xmin = cats$Bwt[i] - 0.6,
    xmax = cats$Bwt[i] + 0.6,
    ymin = cats$Hwt[i] - 0.6,
    ymax = cats$Hwt[i] + 0.6
  )
}</code></pre>
<p>The cat´s paw trail is displaying a linear regression of heart on body weight. This can easily be added by computing a linear Regression, defining a grid to calculate the expected values and plotting cats on top of this data.</p>
<pre><code>LmCat &lt;- lm(Hwt~Bwt, data = cats)

steps &lt;- 20
Reg &lt;- data.frame(Bwt = 
                    seq(from = min(cats$Bwt), 
                            to = max(cats$Bwt), 
                            length.out = steps))
Reg$Hwt &lt;- predict(LmCat, newdata = Reg)</code></pre>
<pre><code>sCATter &lt;- sCATter + 
  geom_point(data = Reg, aes(Bwt, Hwt), size = 0)

paw &lt;- readPNG(&quot;paw.png&quot;)
for (i in 1:nrow(Reg)) {
  sCATter = sCATter +
  annotation_custom(
    rasterGrob(paw),
    xmin = Reg$Bwt[i] - 0.6,
    xmax = Reg$Bwt[i] + 0.6,
    ymin = Reg$Hwt[i] - 0.6,
    ymax = Reg$Hwt[i] + 0.6
  )
}
sCATter</code></pre>
<p>I hope you have as much fun as I did with this one!</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://www.mango-solutions.com/blog/putting-the-cat-in-scatterplot"> Mango Solutions</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/2LfVtvIdn9E" height="1"></a></div>