<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/data-visualisation-pitfalls-how-to-avoid-barbarplots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rtask.thinkr.fr/blog/data-visualisation-pitfalls-how-to-avoid-barbarplots/"> (en) The R Task Force</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>You did an experiment or a survey, or you collected data, and you are ready to analyse them. But what is the first step? Everyone will tell you that you have to visualise your data with graphs. Yes, but how? and why? What are the common pitfalls to avoid?</strong></p>
<p><em>We are very grateful to <a rel="nofollow" href="https://marionlouveaux.fr/" target="_blank">Marion Louveaux, bio-image data analyst</a> for this combination, adaptation and translation of the two French articles <a rel="nofollow" href="https://thinkr.fr/les-pieges-de-la-representation-de-donnees/" target="_blank">“Les pièges de la représentation de données”</a> and <a rel="nofollow" href="https://rtask.thinkr.fr/fr/comment-se-passer-dun-barbarplot/" target="_blank">“Comment se passer d’un barbarplot ?”</a>.</em></p>
<p>Let’s imagine that your dataset contains the sizes of two groups of individuals, A and B. You want to know if the size of the individuals differ between the two groups. In this article, we will show you first an example of a bad approach for data visualisation, and then give you advice on how to proceed in the right way.<br>
<em>The script used to generate this dataset is available at the end of this article.</em></p>
<h1>What not to do: the barbarplot driven analysis</h1>
<h2>The <em>bar-bar-plot</em></h2>
<p>To compare two populations, you probably learned to look at the mean and standard deviation. Your first idea is to do a barplot to represent the mean, with an error bar for the standard deviation. This kind of graph is commonly called a <em>“barbarplot”</em>. To compute a summary variable (here, the mean and the standard deviation) separately for different groups, we use <code>group_by</code> + <code>summarise</code>.</p>
<pre><code>library(tidyverse)
info &lt;- data %>%
  group_by(Group) %>%
  summarise(Mean = mean(Size),
            SD = sd(Size))
ggplot(info, aes(x = Group, y = Mean, fill = Group)) +
  geom_bar(stat = &quot;identity&quot;, position = position_dodge(),
           colour = &quot;grey30&quot;) +
  geom_errorbar(
    aes(ymin = Mean - SD, ymax = Mean + SD),
    width = .2, position = position_dodge(.9), size = 1
  ) + 
  scale_fill_viridis_d()</code></pre>
<p><img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/graphique_bar-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<p>Well, it seems that there is no variation at all here!</p>
<h2>The boxplot</h2>
<p>Just to be sure, you decide then to have a look at the data distribution. You probably learned that a boxplot (<code>geom_boxplot</code>) is usually used to visualise a distribution. Then let’s go!</p>
<pre><code>ggplot(data, aes(x = Group, y = Size, fill = Group)) +
  geom_boxplot(colour = &quot;grey30&quot;) + 
  scale_fill_viridis_d() +
  scale_y_continuous(limits = c(0, NA),
                     expand = c(0, 0)) +
  guides(fill = FALSE)</code></pre>
<p><img src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/graphique_boxplot-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<p>Well, regarding the distributions of sizes, the two populations still seem fairly similar.</p>
<h2>The raw data histogram</h2>
<p>You are very tempted to stop here, but you suddenly remember that distributions can be also visualised with an histogram, even if you find it less easy to read. So let’s do an histogram (<code>geom_histogram</code>) with the raw data.</p>
<pre><code>ggplot(data) + 
  geom_histogram(aes(Size, fill = Group),
                 position = &quot;identity&quot;,
                 alpha = 0.60,
                 bins = 10) +
  scale_fill_viridis_d() +
    scale_y_continuous(limits = c(0, NA),
                     expand = c(0, 0))</code></pre>
<p><img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/histo-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<p><strong>Oooooops!</strong> What the hell is this mess?! The distribution in group B has two peaks!??</p>
<h1>Common pitfalls of graphical representation</h1>
<p>Visualising data before analysing them is important. It helps to identify <strong>trends</strong> and potential <strong>effects</strong>. It also helps looking for extreme values or outliers and deciding to remove them or keep them depending on your question. And it helps choosing the most appropriate statistical tests. Indeed, depending on the type of data (<em>binary</em>, <em>linear</em>, <em>quadratic</em>, <em>bimodal</em>, …), you will not do the same analyses. At that point, you are probably wondering why we are making a new article today, whereas there is already plenty of blog articles about data visualisation, starting with our own <a>ggplot2: welcome viridis</a>. Well, this comes from a simple observation: yes, visualising data with beautiful graphs is a good start, but it is better to choose relevant data visualisation… <img alt="😓" src="https://i1.wp.com/s.w.org/images/core/emoji/11/72x72/1f613.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></p>
<p>Bad choices of graphical representations can lead to graphs that are hard to read, but most importantly they can bias the interpretation. One can see a difference where there is none, or, on the contrary, not see a difference when this one is very important! Or one can have the impression that there are links between the measured variables, when there are not, or the contrary. You don’t want to misunderstand your data, but you also wouldn’t want to present misleading graphs to your readers and take the risk that one may think you are trying to lie about your data, would you?</p>
<p>Regarding misleading graphs, here are a few critics about the barbarplot shown above:</p>
<ol>
<li>the group legend should be removed, as done for the boxplot. There is no point in having the same information in two different places.</li>
<li>the X axis needs to be moved to the zero level of the Y axis because the size of individuals can only be positive. We would not want to lie about the scale of differences…</li>
<li>the so-called error bar is by no means a visual means of representing the result of a possible statistical test of mean comparison: (i) the statistical difference between the means of two groups cannot be detected by comparing the standard deviations of the distributions. (ii) the statistical difference can be represented through the confidence interval of the mean estimate, not through the error bar.</li>
<li>if you ever calculate the standard deviation of the distribution of the mean estimate, you should display a bar about twice the standard deviation on each side of the mean for a representation of the 95% confidence interval.</li>
</ol>
<p><em>By the way, displaying a “p-value” has never been a guarantee of your sincerity and does not give more confidence in misrepresentation</em>.</p>
<p>Remember that a graphical visualisation is a <strong>simplification</strong> of the raw data. You may lose information. It is therefore important to choose an appropriate graphical visualisation. Moreover, graphical representations such as barplots only use summarized statistics of your data, namely the mean/median and standard deviation. The boxplot show additional statistics, but to know the distribution of the data, so nothing is better than an histogram or a violin plot to look at the raw data.</p>
<h1>What to do: the raw data driven analysis</h1>
<h2>Raw data exploration: choosing the appropriate graph</h2>
<p>The first and most important (and usually forgotten) step is to take a look at the distribution of the <strong>raw data</strong>, with one variable at a time.</p>
<h3>Use a barplot (<code>geom_bar</code>) for data counts in categories</h3>
<p>The surface under each bar represent a quantity: the barplot is like a picture from the side of big potato bags into which you put all the objects you wanted to count. If you would not be able to do it for real, the barplot is not the right graphical representation. Here, we can use a barplot to compare the number of individuals in each group, <em>because it would be possible to put all individuals for real in two potato bags</em>.</p>
<pre><code>ggplot(data) + 
  geom_bar(aes(Group, fill = Group), colour = &quot;grey30&quot;) + 
  scale_fill_viridis_d() +
  scale_y_continuous(limits = c(0, NA),
                     expand = c(0, 0)) +
  guides(fill = FALSE)</code></pre>
<p><img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/dogs-cats-barplot_en.png?w=70%25&ssl=1" data-recalc-dims="1"> With this graph, we see that we don’t have the same number of individuals in group A and B.<br>
<em>And yes, we cheated a bit here to put all the individuals in the “bags”, but this is just to give you an example…</em></p>
<h3>Use an histogram (<code>geom_histogram</code>) to represent numerical data</h3>
<p>While one cannot put all size values from all individuals in potato bags, one can put <strong>all individuals of the same size in the same bag</strong>, hence the bars of the histogram.</p>
<pre><code>ggplot(data) + 
  geom_histogram(aes(Size), bins = 30)</code></pre>
<p><img src="https://i0.wp.com/rtask.thinkr.fr/wp-content/uploads/numerical_data-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<p>Here the distribution of the sizes do not correspond to a simple known distribution, but there seems to be at least two groups.</p>
<h2>Data exploration using multiple variables</h2>
<p>Once one know the distribution of each of the variable of the dataset, one can have a look at their behaviour in combination with another variable. This is where we can separate the histogram according to the groups.</p>
<p>For a final graph, the histogram is not the prettiest representation. For a start, we could smooth it a bit with <code>geom_density</code>. <em>Note that the degree of smoothing can be chosen, like the number of classes of a histogram.</em></p>
<pre><code>ggplot(data) + 
  geom_density(aes(Size, fill = Group), 
                 position = &quot;identity&quot;,
                 alpha = 0.60) +
  scale_fill_viridis_d() +
    scale_y_continuous(limits = c(0, NA),
                     expand = c(0, 0))</code></pre>
<p><img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/density-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<h2>The <em>violin plot</em></h2>
<p>The violin plot offers a pretty alternative to the histogram. We can display the previous distribution densities vertically for a better visualisation. And we will add the median as a bonus.</p>
<pre><code>ggplot(data) + 
  geom_violin(aes(Group, Size, fill = Group), 
                 position = &quot;identity&quot;,
              draw_quantiles = c(0.5),
              colour = &quot;grey30&quot;) +
  scale_fill_viridis_d() +
    scale_y_continuous(limits = c(0, NA),
                     expand = c(0, 0)) +
   guides(fill = FALSE)</code></pre>
<p><img src="https://i1.wp.com/rtask.thinkr.fr/wp-content/uploads/violinplot-1.png?w=70%25&ssl=1" data-recalc-dims="1"></p>
<p>One can no longer hide the bimodal aspect of group B. However, in this figure, we can’t see how many individuals are in each group. We wouldn’t want to lie to our readers…? We will now overlay the individuals on the violin plot using <code>geom_dotplot</code>.<br>
Note that if you have few individuals in each group, the <em>violin plot</em> does not always look very good. In this case, you can choose to use only the <code>geom_dotplot</code> with a <code>binwidth</code> adapted to your data.</p>
<pre><code>library(cowplot)
# sample size
sample_size &lt;- data %>% count(Group)
data_size &lt;- data %>% 
  left_join(sample_size, by = &quot;Group&quot;) %>% 
  mutate(myaxis = paste0(Group, &quot;\n&quot;, &quot;n=&quot;, n))
# violin with median
g1 &lt;- ggplot(data_size) + 
  aes(myaxis, Size, fill = Group) +
  geom_violin(position = &quot;identity&quot;,
              draw_quantiles = c(0.5),
              colour = &quot;grey10&quot;, alpha = 0.7,
              scale = &quot;count&quot;) +
    geom_dotplot(method = &quot;histodot&quot;, binaxis = &quot;y&quot;,
                 dotsize = 0.8, binwidth = 10,
                 stackdir = &quot;center&quot;
                 ) +
  scale_fill_viridis_d() +
    scale_y_continuous(limits = c(0, NA),
                       expand = c(0, 0)) +
  xlab(NULL) +
  guides(fill = FALSE)
# dotplot with mean
g2 &lt;- ggplot(data_size) + 
  aes(myaxis, Size, fill = Group) +
    geom_dotplot(method = &quot;histodot&quot;, binaxis = &quot;y&quot;,
                 dotsize = 1, binwidth = 10,
                 stackdir = &quot;center&quot;
                 ) +
  stat_summary(fun.y = &quot;mean&quot;, fun.ymin = &quot;mean&quot;, fun.ymax = &quot;mean&quot;, size = 0.5,
               geom = &quot;crossbar&quot;, colour = &quot;grey30&quot;) +
  scale_fill_viridis_d() +
    scale_y_continuous(limits = c(0, NA),
                       expand = c(0, 0)) +
  xlab(NULL) +
  guides(fill = FALSE)
cowplot::plot_grid(plotlist = list(g1, g2))</code></pre>
<p><img src="https://i2.wp.com/rtask.thinkr.fr/wp-content/uploads/dotplot-1.png?w=85%25&ssl=1" data-recalc-dims="1"></p>
<p>With this kind of data, we know now that we cannot go any further in the statistical analysis without violating the construction assumptions of some tests…</p>
<h2>In conclusion</h2>
<ul>
<li>always look first at the distribution of the <strong>raw data</strong></li>
<li>check the variables one by one, <strong>then</strong> have a look at the relationships between variables</li>
<li>never do a statistical test before the exploration steps mentioned above</li>
<li>for low number of individuals, overlay the points on the graph (for instance use <code>geom_dotplot</code> in combination with <code>geom_violin</code>)</li>
<li>choosing the wrong graphical representation can make people think you are trying to lie about the data. Have a look at the website <a rel="nofollow" href="https://www.data-to-viz.com/" target="_blank">data-to-viz.com</a> to choose an appropriate graphical representation that will not leave any doubt about your intellectual honesty.</li>
</ul>
<p>Do not hesitate to leave your comments and questions at the bottom of the article. We’ll read them carefully.</p>
<h2>Appendix</h2>
<ol>
<li>To create the same fake dataset:</li>
</ol>
<pre><code>set.seed(4321)
data &lt;- tibble(
  Size = c(rnorm(40, 170, 30), rnorm(25, 145, 10), rnorm(25, 195, 10)),
  Group = c(rep(&quot;A&quot;, 40), rep(&quot;B&quot;, 50))
)</code></pre>
<ol start="2">
<li>For the graphs in this article, we defined a default ggplot2 theme using <code>theme_set(theme_classic())</code>.</li>
</ol>
<p>Translated and adapted by <a rel="nofollow" href="https://marionlouveaux.fr/" target="_blank">Marion Louveaux, bio-image data analyst</a></p>
<p>The post <a rel="nofollow" href="https://rtask.thinkr.fr/blog/data-visualisation-pitfalls-how-to-avoid-barbarplots/" target="_blank">Data visualisation pitfalls: how to avoid barbarplots ?</a> appeared first on <a rel="nofollow" href="https://rtask.thinkr.fr/" target="_blank">(en) The R Task Force</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://rtask.thinkr.fr/blog/data-visualisation-pitfalls-how-to-avoid-barbarplots/"> (en) The R Task Force</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/0n8IJetqstA" height="1"></a></div>