<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/day-03-little-helper-multiplot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.statworx.com/de/blog/day-03-little-helper-multiplot/"> r-bloggers – STATWORX</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We at <a rel="nofollow" href="https://www.statworx.com/" target="_blank">STATWORX</a> work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from <a rel="nofollow" href="https://github.com/STATWORX/helfRlein" target="_blank"><code>helfRlein</code></a>. So, on the 3rd day of Christmas my true love gave to me…</p>
<p><img alt="door-03-multiplot" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/door-03-multiplot.png?w=450&ssl=1" class="wp-image-16143" data-recalc-dims="1"></p>
<h2>What can it do?</h2>
<p>This little helper combines multiple ggplots into one plot. This is a function taken from <a rel="nofollow" href="http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/" target="_blank">the R cookbook</a>.</p>
<p>An advantage over <code>facets</code> is, that you don't need all data for all plots within one object. Also you can freely create each single plot – which can sometimes also be a disadvantage.</p>
<p>With the <code>layout</code> parameter you can arrange multiple plots with different sizes. Let's say you have three plots and want to arrange them like this:</p>
<pre><code lang="r">1    2    2
1    2    2
3    3    3
</code></pre>
<p>With <code>multiplot</code> it boils down to</p>
<pre><code>multiplot(plotlist = list(p1, p2, p3),
          layout = matrix(c(1,2,2,1,2,2,3,3,3), nrow = 3, byrow = TRUE))
</code></pre>
<p><img alt="day-03-multiplot" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/day-03-multiplot.png?w=450&ssl=1" class="wp-image-16166" data-recalc-dims="1"></p>
<h2>Code for plot example</h2>
<pre><code># star coordinates
c1	=	cos((2*pi)/5)	
c2	=	cos(pi/5)
s1	=	sin((2*pi)/5)
s2	=	sin((4*pi)/5)

data_star &lt;- data.table(X = c(0, -s2, s1, -s1, s2),
                        Y = c(1, -c2, c1, c1, -c2))

p1 &lt;- ggplot(data_star, aes(x = X, y = Y)) +
  geom_polygon(fill = &quot;gold&quot;) +
  theme_void()

# tree
set.seed(24122018)
n &lt;- 10000
lambda &lt;- 2
data_tree &lt;- data.table(X = c(rpois(n, lambda), rpois(n, 1.1*lambda)),
                        TYPE = rep(c(&quot;1&quot;, &quot;2&quot;), each = n))
data_tree &lt;- data_tree[, list(COUNT = .N), by = c(&quot;TYPE&quot;, &quot;X&quot;)]
data_tree[TYPE == &quot;1&quot;, COUNT := -COUNT]

p2 &lt;- ggplot(data_tree, aes(x = X, y = COUNT, fill = TYPE)) +
  geom_bar(stat = &quot;identity&quot;) +
  scale_fill_manual(values = c(&quot;green&quot;, &quot;darkgreen&quot;)) +
  coord_flip() +
  theme_minimal()

# gifts
data_gifts &lt;- data.table(X = runif(5, min = 0, max = 10),
                         Y = runif(5, max = 0.5),
                         Z = sample(letters[1:5], 5, replace = FALSE))

p3 &lt;- ggplot(data_gifts, aes(x = X, y = Y)) +
  geom_point(aes(color = Z), pch = 15, size = 10) +
  scale_color_brewer(palette = &quot;Reds&quot;) +
  geom_point(pch = 12, size = 10, color = &quot;gold&quot;) +
  xlim(0,8) +
  ylim(0.1,0.5) +
  theme_minimal() + 
  theme(legend.position=&quot;none&quot;) 
</code></pre>
<h2>Overview</h2>
<p>To see all the other functions you can either check out our <a rel="nofollow" href="https://github.com/STATWORX/helfRlein" target="_blank">GitHub</a> or you can read about them <a rel="nofollow" href="https://www.statworx.com/tag/helfrlein/" target="_blank">here</a>.</p>
<p>Have a merry advent season!</p>
<div>
<h6>Über den Autor</h6>
<p><img src="https://i1.wp.com/www.statworx.com/wp-content/uploads/jakob-1-300x300.jpg?resize=180%2C180&ssl=1" width="180" alt="Jakob Gepp" class="wp-user-avatar wp-user-avatar-180" data-recalc-dims="1" height="180"></p>
<div>
<h4>Jakob Gepp</h4>
<p>Numbers were always my passion and as a data scientist and statistician at <a rel="nofollow" href="https://www.statworx.com/" target="_blank">STATWORX</a> I can fullfill my nerdy needs. Also I am responsable for our blog. So if you have any questions or suggestions, just send me an email!</p>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<h1>ABOUT US</h1>
</div>
</div>
</div>
<hr>
<div>
<p><span><span>STATWORX</span><br></span><span>is a consulting company for data science, statistics, machine learning and artificial intelligence </span><span>located in Frankfurt, Zurich and Vienna. Sign up for our NEWSLETTER and receive reads and treats from the world of data science and AI.</span> </p>
</div>
<p><a data-x-toggle="1" tabindex="0" data-x-toggleable="e15819-7">
<p> <span>
<p><span>
<p><i data-x-icon-o=""></i></p>
<p> <span><span>Sign Up Now!</span></span> </p></span>
<p>




<p>Der Beitrag <a rel="nofollow" href="https://www.statworx.com/de/blog/day-03-little-helper-multiplot/" target="_blank">Day 03 – little helper multiplot</a> erschien zuerst auf <a rel="nofollow" href="https://www.statworx.com/de/" target="_blank">STATWORX</a>.</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.statworx.com/de/blog/day-03-little-helper-multiplot/"> r-bloggers – STATWORX</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/cyzn9HSg4-g" height="1"></a></div></span></a></div></div></div></div>