<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/a-not-so-simple-bar-plot-example-using-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.inwt-statistics.com/read-blog/a-not-so-simple-bar-plot-example-using-ggplot2-507.html"> INWT-Blog-RBloggers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>This is a reproduction of the (simple) bar plot of chapter 6.1.1 in <a rel="nofollow" href="http://www.datendesign-r.de/" target="_blank">Datendesign mit R</a> with <a rel="nofollow" href="http://ggplot2.org/" target="_blank">ggplot2</a>. To download the data you can use the following lines:</p>
</div>
<pre><pre><code>dir.create(&quot;data&quot;) writeLines(&quot;*&quot;, &quot;data/.gitignore&quot;) download.file(&quot;http://www.datendesign-r.de/alle_daten.zip&quot;,               &quot;data/alle_daten.zip&quot;) unzip(&quot;data/alle_daten.zip&quot;, exdir = &quot;data&quot;) </code></pre></pre>
<div>
<p><span>And to download the original script and the base R version of this plot:</span></p>
</div>
<pre><pre><code>download.file(&quot;http://www.datendesign-r.de/beispielcode.zip&quot;,               &quot;data/beispielcode.zip&quot;) unzip(&quot;data/beispielcode.zip&quot;, exdir = &quot;data&quot;) </code></pre></pre>
<div>
<p>After downloading check out the original pdf version of this plot in <em>data/beispielcode/pdf/balkendiagramm_einfach.pdf</em>.</p>
</div>
<div>
<h2>Preparing Data</h2>
<p><span>Here are some steps to modify the data such that it can be easily used with ggplot2.</span></p>
</div>
<pre><pre><code># remember to adjust the path ipsos &lt;- openxlsx::read.xlsx(&quot;../data/alle_daten/ipsos.xlsx&quot;)  ipsos &lt;- ipsos[order(ipsos$Wert),] ipsos$Land &lt;- ordered(ipsos$Land, ipsos$Land) ipsos$textFamily &lt;- ifelse(ipsos$Land %in% c(&quot;Deutschland&quot;,&quot;Brasilien&quot;),                            &quot;Lato Black&quot;, &quot;Lato Light&quot;) ipsos$labels &lt;- paste0(ipsos$Land, ifelse(ipsos$Wert &lt; 10, &quot;     &quot;, &quot;  &quot;),                        ipsos$Wert) rect &lt;- data.frame(   ymin = seq(0, 80, 20),   ymax = seq(20, 100, 20),   xmin = 0.5, xmax = 16.5,   colour = rep(c(grDevices::rgb(191,239,255,80,maxColorValue=255),                  grDevices::rgb(191,239,255,120,maxColorValue=255)),                length.out = 5)) </code></pre></pre>
<div>
<h2>The Basic Plot</h2>
<p>First we add the geoms, then modifications to the scales and flip of the coordinate system. The remaining code is just modifying the appearance.</p>
</div>
<pre><pre><code>library(&quot;ggplot2&quot;) ggBar &lt;- ggplot(ipsos) +   geom_bar(aes(x = Land, y = Wert), stat = &quot;identity&quot;, fill = &quot;grey&quot;) +   geom_bar(aes(x = Land, y = ifelse(Land %in% c(&quot;Brasilien&quot;, &quot;Deutschland&quot;), Wert, NA)),            stat = &quot;identity&quot;, fill = rgb(255,0,210,maxColorValue=255)) +   geom_rect(data = rect,             mapping = aes(ymin = ymin, ymax = ymax,                           xmin = xmin, xmax = xmax),             fill = rect$colour) +   geom_hline(aes(yintercept = 45), colour = &quot;skyblue3&quot;) +   scale_y_continuous(breaks = seq(0, 100, 20), limits = c(0, 100), expand = c(0, 0)) +   scale_x_discrete(labels = ipsos$labels) +     coord_flip() +   labs(y = NULL,        x = NULL,        title = NULL) +   theme_minimal() +   theme(panel.grid.minor = element_blank(),         panel.grid.major = element_blank(),         axis.ticks = element_blank(),         axis.text.y = element_text(           family = ipsos$textFamily),         text = element_text(family = &quot;Lato Light&quot;)) ggBar </code></pre></pre>
<div>
<p><img alt="" src="https://i2.wp.com/www.inwt-statistics.com/files/INWT/images_blog/ANotSoSimpleBarPlotExampleUsingGgplot2/basic_plot.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Annotations and Layout</h2>
<p>Of course you can simply add the title and text annotations to the plot using ggplot2, but I didn’t find a way to do the exact placement comparable to the original version without the package grid.</p>
</div>
<pre><pre><code>library(&quot;grid&quot;) vp_make &lt;- function(x, y, w, h)    viewport(x = x, y = y, width = w, height = h, just = c(&quot;left&quot;, &quot;bottom&quot;)) main &lt;- vp_make(0.05, 0.05, 0.9, 0.8) title &lt;- vp_make(0, 0.9, 0.6, 0.1) subtitle &lt;- vp_make(0, 0.85, 0.4, 0.05) footnote &lt;- vp_make(0.55, 0, 0.4, 0.05) annotation1 &lt;- vp_make(0.7, 0.85, 0.225, 0.05) annotation2 &lt;- vp_make(0.4, 0.85, 0.13, 0.05) # To see which space these viewports will use: grid.rect(gp = gpar(lty = &quot;dashed&quot;)) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = main) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = title) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = subtitle) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = footnote) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = annotation1) grid.rect(gp = gpar(col = &quot;grey&quot;), vp = annotation2)</code></pre></pre>
<div>
<p><img alt="" src="https://i0.wp.com/www.inwt-statistics.com/files/INWT/images_blog/ANotSoSimpleBarPlotExampleUsingGgplot2/Layout.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<p><span>And now we can add the final annotations to the plot:</span></p>
</div>
<pre><pre><code># pdf_datei&lt;-&quot;balkendiagramme_einfach.pdf&quot; # cairo_pdf(bg = &quot;grey98&quot;, pdf_datei, width=9, height=6.5) grid.newpage() print(ggBar, vp = main) </code></pre></pre>
<pre><pre><code>grid.text(&quot;'Ich glaube fest an Gott oder ein höheres Wesen'&quot;,           gp = gpar(fontfamily = &quot;Lato Black&quot;, fontsize = 14),           just = &quot;left&quot;, x = 0.05, vp = title) grid.text(&quot;...sagten 2010 in:&quot;,           gp = gpar(fontfamily = &quot;Lato Light&quot;, fontsize = 12),           just = &quot;left&quot;,           x = 0.05, vp = subtitle) grid.text(&quot;Quelle: www.ipsos-na.com, Design: Stefan Fichtel, ixtract&quot;,           gp = gpar(fontfamily = &quot;Lato Light&quot;, fontsize = 9),           just = &quot;right&quot;,           x = 0.95, vp = footnote) grid.text(&quot;Alle Angaben in Prozent&quot;,           gp = gpar(fontfamily = &quot;Lato Light&quot;, fontsize = 9),           just = &quot;right&quot;,           x = 1, y = 0.55, vp = annotation1) grid.text(&quot;Durchschnitt: 45&quot;,           gp = gpar(fontfamily = &quot;Lato Light&quot;, fontsize = 9),           just = &quot;right&quot;,           x = 0.95, y = 0.55, vp = annotation2) </code></pre></pre>
<div>
<p><img alt="" src="https://i2.wp.com/www.inwt-statistics.com/files/INWT/images_blog/ANotSoSimpleBarPlotExampleUsingGgplot2/balkendiagramm_ggplot2.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<pre><pre><code># dev.off()</code></pre></pre>
	<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.inwt-statistics.com/read-blog/a-not-so-simple-bar-plot-example-using-ggplot2-507.html"> INWT-Blog-RBloggers</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/1w0pqQXoHgM" height="1"></a></div>