<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/bar-plots-and-modern-alternatives/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives"> Easy Guides</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p>  </p>
<div>
<p></p>
<p>This article describes how to create easily basic and ordered <strong>bar plots</strong> using ggplot2 based helper functions available in the <a rel="nofollow" href="http://www.sthda.com/english/rpkgs/ggpubr/index.html" target="_blank">ggpubr R package</a>. We’ll also present some modern alternatives to bar plots, including <strong>lollipop charts</strong> and <strong>cleveland’s dot plots</strong>.</p>
<div>
<p>
Note that, the approach to build a bar plot, using ggplot2 standard verbs, has been described in our previous article available at: <a rel="nofollow" href="http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization" target="_blank">ggplot2 barplots : Quick start guide</a>.
</p>
<p>
You might be also interested by the following articles:
</p>
<ul>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots" target="_blank">Add P-values and Significance Levels to ggplots</a>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/facilitating-exploratory-data-visualization-application-to-tcga-genomic-data" target="_blank">Facilitating Exploratory Data Visualization: Application to TCGA Genomic Data</a>
</li>
</ul>
</div>
<p></p>
<div>
<img alt="Bar plots and modern alternatives" src="https://i0.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-logo-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<p><strong>Contents</strong>:</p>
<div>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#prerequisites" target="_blank">Prerequisites</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#basic-bar-plots" target="_blank">Basic bar plots</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#multiple-grouping-variables" target="_blank">Multiple grouping variables</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#ordered-bar-plots" target="_blank">Ordered bar plots</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#deviation-graphs" target="_blank">Deviation graphs</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#alternatives-to-bar-plots" target="_blank">Alternatives to bar plots</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives#infos" target="_blank">Infos</a></li>
</ul>
</div>
<div>
<h2>Prerequisites</h2>
<div>
<h3>Required R package</h3>
<p>You need to install the R package <a rel="nofollow" href="http://www.sthda.com/english/rpkgs/ggpubr" target="_blank">ggpubr (version >= 0.1.3)</a>, to easily create ggplot2-based publication ready plots.</p>
<p>Install from CRAN:</p>
<pre><code>install.packages(&quot;ggpubr&quot;)</code></pre>
<p>Or, install the latest developmental version from <a rel="nofollow" href="https://github.com/kassambara/ggpubr" target="_blank">GitHub</a> as follow:</p>
<pre><code>if(!require(devtools)) install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;kassambara/ggpubr&quot;)</code></pre>
<p>Load ggpubr:</p>
<pre><code>library(ggpubr)</code></pre>
</div>
</div>
<div>
<h2>Basic bar plots</h2>
<p>Create a demo data set:</p>
<pre><code>df &lt;- data.frame(dose=c(&quot;D0.5&quot;, &quot;D1&quot;, &quot;D2&quot;),
                 len=c(4.2, 10, 29.5))
print(df)</code></pre>
<pre><code>  dose  len
1 D0.5  4.2
2   D1 10.0
3   D2 29.5</code></pre>
<p>Basic bar plots:</p>
<pre><code># Basic bar plots with label
p &lt;- ggbarplot(df, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;black&quot;, fill = &quot;lightgray&quot;)
p

# Rotate to create horizontal bar plots
p + rotate()</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-basics-1.png?w=336" data-recalc-dims="1"><img alt="Bar plots and modern alternatives" src="https://i0.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-basics-2.png?w=336" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<p>Change fill and outline colors by groups:</p>
<pre><code>ggbarplot(df, x = &quot;dose&quot;, y = &quot;len&quot;,
   fill = &quot;dose&quot;, color = &quot;dose&quot;, palette = &quot;jco&quot;)</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-color-1.png?w=336" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
<div>
<h2>Multiple grouping variables</h2>
<p>Create a demo data set:</p>
<pre><code>df2 &lt;- data.frame(supp=rep(c(&quot;VC&quot;, &quot;OJ&quot;), each=3),
                  dose=rep(c(&quot;D0.5&quot;, &quot;D1&quot;, &quot;D2&quot;),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))
print(df2)</code></pre>
<pre><code>  supp dose  len
1   VC D0.5  6.8
2   VC   D1 15.0
3   VC   D2 33.0
4   OJ D0.5  4.2
5   OJ   D1 10.0
6   OJ   D2 29.5</code></pre>
<p>Plot y = “len” by x = “dose” and change color by a second group: “supp”</p>
<pre><code># Stacked bar plots, add labels inside bars
ggbarplot(df2, x = &quot;dose&quot;, y = &quot;len&quot;,
  fill = &quot;supp&quot;, color = &quot;supp&quot;, 
  palette = c(&quot;gray&quot;, &quot;black&quot;),
  label = TRUE, lab.col = &quot;white&quot;, lab.pos = &quot;in&quot;)

# Change position: Interleaved (dodged) bar plot
ggbarplot(df2, x = &quot;dose&quot;, y = &quot;len&quot;,
          fill = &quot;supp&quot;, color = &quot;supp&quot;, 
          palette = c(&quot;gray&quot;, &quot;black&quot;),
          position = position_dodge(0.9))</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i0.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-stacked-bar-plots-1.png?w=336" data-recalc-dims="1"><img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-stacked-bar-plots-2.png?w=336" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
<div>
<h2>Ordered bar plots</h2>
<p>Load and prepare data:</p>
<pre><code># Load data
data(&quot;mtcars&quot;)
dfm &lt;- mtcars
# Convert the cyl variable to a factor
dfm$cyl &lt;- as.factor(dfm$cyl)
# Add the name colums
dfm$name &lt;- rownames(dfm)
# Inspect the data
head(dfm[, c(&quot;name&quot;, &quot;wt&quot;, &quot;mpg&quot;, &quot;cyl&quot;)])</code></pre>
<pre><code>                               name    wt  mpg cyl
Mazda RX4                 Mazda RX4 2.620 21.0   6
Mazda RX4 Wag         Mazda RX4 Wag 2.875 21.0   6
Datsun 710               Datsun 710 2.320 22.8   4
Hornet 4 Drive       Hornet 4 Drive 3.215 21.4   6
Hornet Sportabout Hornet Sportabout 3.440 18.7   8
Valiant                     Valiant 3.460 18.1   6</code></pre>
<p>Create ordered bar plots. Change the fill color by the grouping variable “cyl”. Sorting will be done globally, but not by groups.</p>
<pre><code>ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
          fill = &quot;cyl&quot;,               # change fill color by cyl
          color = &quot;white&quot;,            # Set bar border colors to white
          palette = &quot;jco&quot;,            # jco journal color palett. see ?ggpar
          sort.val = &quot;desc&quot;,          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-ordered-bar-plots-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<p>Sort bars inside each group. Use the argument <strong>sort.by.groups = TRUE</strong>.</p>
<pre><code>ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
          fill = &quot;cyl&quot;,               # change fill color by cyl
          color = &quot;white&quot;,            # Set bar border colors to white
          palette = &quot;jco&quot;,            # jco journal color palett. see ?ggpar
          sort.val = &quot;asc&quot;,           # Sort the value in dscending order
          sort.by.groups = TRUE,      # Sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-ordered-bar-plots-by-groups-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
<div>
<h2>Deviation graphs</h2>
<p>The deviation graph shows the deviation of quantitative values to a reference value. In the R code below, we’ll plot the mpg z-score from the mtcars data set.</p>
<p>Calculate the z-score of the mpg data:</p>
<pre><code># Calculate the z-score of the mpg data
dfm$mpg_z &lt;- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp &lt;- factor(ifelse(dfm$mpg_z &lt; 0, &quot;low&quot;, &quot;high&quot;), 
                     levels = c(&quot;low&quot;, &quot;high&quot;))
# Inspect the data
head(dfm[, c(&quot;name&quot;, &quot;wt&quot;, &quot;mpg&quot;, &quot;mpg_z&quot;, &quot;mpg_grp&quot;, &quot;cyl&quot;)])</code></pre>
<pre><code>                               name    wt  mpg      mpg_z mpg_grp cyl
Mazda RX4                 Mazda RX4 2.620 21.0  0.1508848    high   6
Mazda RX4 Wag         Mazda RX4 Wag 2.875 21.0  0.1508848    high   6
Datsun 710               Datsun 710 2.320 22.8  0.4495434    high   4
Hornet 4 Drive       Hornet 4 Drive 3.215 21.4  0.2172534    high   6
Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345     low   8
Valiant                     Valiant 3.460 18.1 -0.3302874     low   6</code></pre>
<p>Create an ordered bar plot, colored according to the level of mpg:</p>
<pre><code>ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
          fill = &quot;mpg_grp&quot;,           # change fill color by mpg_level
          color = &quot;white&quot;,            # Set bar border colors to white
          palette = &quot;jco&quot;,            # jco journal color palett. see ?ggpar
          sort.val = &quot;asc&quot;,           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = &quot;MPG z-score&quot;,
          xlab = FALSE,
          legend.title = &quot;MPG Group&quot;
          )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-deviation-graphs-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<p>Rotate the plot: use rotate = TRUE and sort.val = “desc”</p>
<pre><code>ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
          fill = &quot;mpg_grp&quot;,           # change fill color by mpg_level
          color = &quot;white&quot;,            # Set bar border colors to white
          palette = &quot;jco&quot;,            # jco journal color palett. see ?ggpar
          sort.val = &quot;desc&quot;,          # Sort the value in descending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = &quot;MPG z-score&quot;,
          legend.title = &quot;MPG Group&quot;,
          rotate = TRUE,
          ggtheme = theme_minimal()
          )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-deviation-graphs-horizontal-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
<div>
<h2>Alternatives to bar plots</h2>
<div>
<h3>Lollipop chart</h3>
<p>Lollipop chart is an alternative to bar plots, when you have a large set of values to visualize.</p>
<p>Lollipop chart colored by the grouping variable “cyl”:</p>
<pre><code>ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
           color = &quot;cyl&quot;,                                # Color by groups
           palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
           sorting = &quot;ascending&quot;,                        # Sort value in descending order
           add = &quot;segments&quot;,                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
           )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-lollipop-chart-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<ul>
<li>Sort in descending order. <strong>sorting = “descending”</strong>.</li>
<li>Rotate the plot vertically, using <strong>rotate = TRUE</strong>.</li>
<li>Sort the mpg value inside each group by using <strong>group = “cyl”</strong>.</li>
<li>Set <strong>dot.size</strong> to 6.</li>
<li>Add mpg values as label. <strong>label = “mpg”</strong> or <strong>label = round(dfm$mpg)</strong>.</li>
</ul>
<pre><code>ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
           color = &quot;cyl&quot;,                                # Color by groups
           palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
           sorting = &quot;descending&quot;,                       # Sort value in descending order
           add = &quot;segments&quot;,                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = &quot;cyl&quot;,                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = &quot;white&quot;, size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-lollipop-chart-rotate-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
<p>Deviation graph:</p>
<ul>
<li>Use y = “mpg_z”</li>
<li>Change segment color and size: add.params = list(color = “lightgray”, size = 2)</li>
</ul>
<pre><code>ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
           color = &quot;cyl&quot;,                                # Color by groups
           palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
           sorting = &quot;descending&quot;,                       # Sort value in descending order
           add = &quot;segments&quot;,                             # Add segments from y = 0 to dots
           add.params = list(color = &quot;lightgray&quot;, size = 2), # Change segment color and size
           group = &quot;cyl&quot;,                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg_z,1),                        # Add mpg values as dot labels
           font.label = list(color = &quot;white&quot;, size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  geom_hline(yintercept = 0, linetype = 2, color = &quot;lightgray&quot;)</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i0.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-lollipop-chart-deviation-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
<div>
<h3>Cleveland’s dot plot</h3>
<p>Color y text by groups. Use y.text.col = TRUE.</p>
<pre><code>ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
           color = &quot;cyl&quot;,                                # Color by groups
           palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
           sorting = &quot;descending&quot;,                       # Sort value in descending order
           rotate = TRUE,                                # Rotate vertically
           dot.size = 2,                                 # Large dot size
           y.text.col = TRUE,                            # Color y text by groups
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  theme_cleveland()                                      # Add dashed grids</code></pre>
<div>
<img alt="Bar plots and modern alternatives" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/figure/ggpubr/bar-plots-and-alternatives-cleveland-dot-plots-1.png?w=450" data-recalc-dims="1">
<p>
Bar plots and modern alternatives
</p>
</div>
</div>
</div>
<div>
<h2>Infos</h2>
<p>This analysis has been performed using <strong>R software</strong> (ver. 3.3.2) and <strong>ggpubr</strong> (ver. 0.1.4).</p>
</div>
<p></p>

</div>
<p><br>
</p>
<p></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="http://www.sthda.com/english/wiki/bar-plots-and-modern-alternatives"> Easy Guides</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/RB-OmIKY3_c" height="1"></a></div>