How to Make Stunning Bar Charts in R: A Complete Guide with ggplot2

Estimated time:
time
min

<h2><span data-preserver-spaces="true">Bar Charts with R </span></h2> <em><strong>Updated</strong>: May 6, 2022.</em> <span data-preserver-spaces="true">The language of data visualization is universal. Not everyone will recognize a great visualization, but everyone will remember a terrible one. As it turns out, the R programming language is amazing for visualizing data. Today you'll learn how to make stunning ggplot 2 bar charts with ease. If you use the tools and techniques discussed in this article, the chances for your visualization to be classified as "terrible" will be close to zero.</span> <blockquote>Want to make your workflow more productive? <a href="https://appsilon.com/rstudio-shortcuts-and-tips/" target="_blank" rel="noopener noreferrer">Here's our curated list of RStudio shortcuts and tips</a>.</blockquote> <span data-preserver-spaces="true">This article shows you how to make all sorts of bar charts with R and <code>ggplot2</code>. You'll also learn how to make them aesthetically pleasing with colors, themes, titles, and labels. </span> <span data-preserver-spaces="true">Today you'll learn how to:</span> <ul><li><a href="#first-bar-chart">Make your first bar chart</a></li><li><a href="#colors-and-themes">Change colors and themes</a></li><li><a href="#titles-subtitles-captions">Add titles, subtitles, and captions</a></li><li><a href="#axis-labels">Edit axis labels</a></li><li><a href="#stacked-grouped-horizontal">Make stacked, grouped, and horizontal bar charts</a></li><li><a href="#labels">Add labels</a></li><li><a href="#lines">Add lines</a></li></ul> <hr /> <h2 id="first-bar-chart"><span data-preserver-spaces="true">Make Your First ggplot2 Bar Chart</span></h2> <span data-preserver-spaces="true">There are plenty of datasets built into R and thousands of others available online. Still, you'll declare your own. The reasoning is simple - you're here to learn how to make bar charts, not how to aggregate data.</span> <span data-preserver-spaces="true">Here's the dataset you'll use today:</span> <script src="https://gist.github.com/darioappsilon/fa1af1f12e60bf5ee507e46b874f8822.js"></script> <span style="margin-top: 25px !important;">R's standard library for data visualization is <code>ggplot2</code>. It's based on the layering principle. For example, you first declare a data layer and then a visualization layer. These two are mandatory for any visualization. You'll see later how additional layers can make charts more informative and appealing.</span> <span data-preserver-spaces="true">To start, you'll make a bar chart that has the column <code>quarter</code> on the x-axis and <code>profit</code> on the y-axis. That's declared in the first layer (data), and the second layer (visualization) specifies which type of visualization you want. The <code>geom_bar</code> and <code>geom_col</code> layers are used to create bar charts. With the first option, you need to specify <code>stat = "identity"</code> for it to work, so the ladder is used throughout the article.</span> <span data-preserver-spaces="true">You can create a simple bar chart with this code:</span> <script src="https://gist.github.com/darioappsilon/3cefc7ee4b6448666faeb1ac00d708bb.js"></script> <span data-preserver-spaces="true">Here's the corresponding visualization:</span> <img class="size-full wp-image-6126" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5dc179ba2a439c0f5e3_47cfd3ee_1.webp" alt="Simple bar chart" width="1325" height="893" /> Image 1 - Simple bar chart <span data-preserver-spaces="true">This one gets the job done but doesn't look like something you'd want to show to your boss. You'll fix it in the following sections.</span> <h2 id="colors-and-themes"><span data-preserver-spaces="true">Add Colors and Themes to ggplot2 Bar Charts</span></h2> <span data-preserver-spaces="true">Tweaking colors and themes is the simplest thing you can do to make visualization look better. The <code>geom_bar()</code> has two useful parameters:</span> <ul><li><span data-preserver-spaces="true"><code>color</code> - outline color of the bars</span></li><li><span data-preserver-spaces="true"><code>fill</code> - fill color of the bars</span></li></ul> <span data-preserver-spaces="true">Here's how to use <code>fill</code> to make your chart Appsilon-approved:</span> <script src="https://gist.github.com/darioappsilon/8a5b03c142a14b2ec2a82e09ad9e6b5d.js"></script> <img class="size-full wp-image-6127" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5dd842ddef80cb868f4_d39f2f57_2.webp" alt="Using fill to change the bar color" width="1320" height="893" /> Image 2 - Using fill to change the bar color <span data-preserver-spaces="true">The <code>color</code> parameter changes only the outline. The dataset you're using has two distinct products. R draws a fill line between products' values, as stacked bar charts are used by default. You'll learn more about the stacked charts later.</span> <span data-preserver-spaces="true">Here's what this means in practice. The code snippet below sets the fill color to white and the outline color to blue:</span> <script src="https://gist.github.com/darioappsilon/e42f4df496d039e41975c509fefa1312.js"></script> <img class="size-full wp-image-6128" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5de13d4a2a1c0fb5c91_6aeada88_3.webp" alt="Changing the outline color" width="1319" height="893" /> Image 3 - Changing the outline color <span data-preserver-spaces="true">In case coloring doesn't do the trick, you can completely change the theme. That's yet another layer to add after the initial visualization layer. Here's how to do it:</span> <script src="https://gist.github.com/darioappsilon/5df1e2209b942aa795b8f0ba89d466b0.js"></script> <div class="mceTemp"></div> <img class="size-full wp-image-6129" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5de2210fb3996719f25_afff178d_4.webp" alt="Changing the visualization theme" width="1319" height="893" /> Image 4 - Changing the visualization theme <span data-preserver-spaces="true">If this theme isn't your thing, there's plenty more to pick from. You can find the entire list </span><a class="editor-rtfLink" href="https://ggplot2.tidyverse.org/reference/ggtheme.html" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">here</span></a><span data-preserver-spaces="true">.</span> <h2 id="titles-subtitles-captions"><span data-preserver-spaces="true">Include Titles, Subtitles, and Captions in Your ggplot2 Bar Charts</span></h2> <span data-preserver-spaces="true">A visualization without a title is useless. There's no way to know if you're looking at </span><em><span data-preserver-spaces="true">Election votes</span></em><span data-preserver-spaces="true"> or </span><em><span data-preserver-spaces="true">2020 USA election votes in California</span></em><span data-preserver-spaces="true">. You can use subtitles to put additional information, but it's not mandatory. Captions are useful for placing visualization credits and sources.</span> <span data-preserver-spaces="true">The most convenient way to add these is through a <code>labs()</code> layer. It takes in values for <code>title</code>, <code>subtitle</code>, and <code>caption</code>. </span> <span data-preserver-spaces="true">Let's see how to add all three:</span> <script src="https://gist.github.com/darioappsilon/7b0554aaa717b5c0ba4d0e69c2b37868.js"></script> <img class="size-full wp-image-6130" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5dff9a88e2eece1f7c7_6b563570_5.webp" alt="Title, subtitle, and caption with default styles" width="1319" height="893" /> Image 5 - Title, subtitle, and caption with default styles <span data-preserver-spaces="true">It's a good start, but what if you want to </span><strong><span data-preserver-spaces="true">add styles</span></strong><span data-preserver-spaces="true">? Let's see how to color the title, bold the subtitle, and italicize the caption:</span> <script src="https://gist.github.com/darioappsilon/add0deb8e351a2d2afaf0b15550e116f.js"></script> <img class="size-full wp-image-6131" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5dfcba09eb752814231_f34e2de2_6.webp" alt="Styling title, subtitle, and caption" width="1319" height="893" /> Image 6 - Styling title, subtitle, and caption <span data-preserver-spaces="true">Let's take this a step further. Here's how to align the title to the middle, subtitle to the right, and caption to the left:</span> <script src="https://gist.github.com/darioappsilon/54ed7d577b5aa3602e9839e3ea7bead8.js"></script> <img class="size-full wp-image-6132" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e02624c21972a03009_ab82c72f_7.webp" alt="Aligning title, subtitle, and caption" width="1319" height="893" /> Image 7 - Aligning title, subtitle, and caption <span data-preserver-spaces="true">You've learned how to add a nicely-formatted title, but the default axis labels still holds your visualization back. You'll learn how to change them next.</span> <h2 id="axis-labels"><span data-preserver-spaces="true">Don't ForAxis Labels</span></h2> <span data-preserver-spaces="true">Long story short - it works identically as with titles and subtitles. The <code>labs()</code> layer takes in values for both X and Y-axis labels.</span> <span data-preserver-spaces="true">Here's how to change the text:</span> <script src="https://gist.github.com/darioappsilon/a0286f3ccd87edeae623e3e9dcb73369.js"></script> <img class="size-full wp-image-6133" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e025b5dfc1d5c87c52_8bcc9c82_8.webp" alt="Changing X and Y axis labels" width="1319" height="893" /> Image 8 - Changing X and Y axis labels <span data-preserver-spaces="true">You can change the styles the same way you did with titles, subtitles, and captions. The following code snippet will make your x-axis label blue and bold, and y-axis label italic:</span> <script src="https://gist.github.com/darioappsilon/e5d059d4748ed1fb5e50135ddba2ad41.js"></script> <img class="size-full wp-image-6134" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e1d2619e892f97ad22_bcbb4e1e_9.webp" alt="Changing stylings of X and Y axis labels" width="1319" height="893" /> Image 9 - Changing stylings of X and Y axis labels <span data-preserver-spaces="true">And that does it for changing the basic visuals. You'll learn how to work with different bar charts next - stacked, grouped, and horizontal.</span> <h2 id="stacked-grouped-horizontal"><span data-preserver-spaces="true">Stacked, Grouped, and Horizontal Bar Charts</span></h2> <span data-preserver-spaces="true">The <code>ggplot2</code> package uses stacked bar charts by default. Stacked bar charts are best used when all portions are colored differently.</span> <span data-preserver-spaces="true">To change the coloring, you only need to change the <code>fill</code> value in the data layer. Here's an example:</span> <script src="https://gist.github.com/darioappsilon/75ef1f3eb6010974540ee00311a7ad52.js"></script> <img class="size-full wp-image-6135" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e1179ba2a439c0f726_1ad84f06_10.webp" alt="Default stacked bar chart" width="1319" height="893" /> Image 10 - Default stacked bar chart <span data-preserver-spaces="true">There's a visible distinction between products, and you can now see how much profit each product made quarterly. </span> <span data-preserver-spaces="true">There are two ways to change each portion's color:</span> <ul><li><strong><span data-preserver-spaces="true">Manually</span></strong><span data-preserver-spaces="true"> - by specifying a vector of color names or color hex codes</span></li><li><strong><span data-preserver-spaces="true">With palettes</span></strong><span data-preserver-spaces="true"> - by using built-in color palettes</span></li></ul> <span data-preserver-spaces="true">Let's cover the manual approach first. You have to add a layer with <code>scale_fill_manual</code>:</span> <script src="https://gist.github.com/darioappsilon/541613e5676836c680d6c22df93e25fc.js"></script> <img class="size-full wp-image-6137" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e25beedd53eb02c58c_53d61d74_11.webp" alt="Stacked bar chart with custom colors" width="1319" height="893" /> Image 11 - Stacked bar chart with custom colors <span data-preserver-spaces="true">Palettes are a bit easier because you don't need to know exact color values. For the same reason, it can also be considered as a limitation. </span><a class="editor-rtfLink" href="http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually#use-rcolorbrewer-palettes" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">Here's a list</span></a><span data-preserver-spaces="true"> of built-in palettes. The <code>scale_fill_brewer</code> layer is used to work with palettes:</span> <script src="https://gist.github.com/darioappsilon/f93eea60c649807f2ebc9ea149e8c722.js"></script> <img class="size-full wp-image-6138" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b270bb17c4b0497b83251b_12.webp" alt="Stacked bar chart colored with a built-in palette" width="1319" height="893" /> Image 12 - Stacked bar chart colored with a built-in palette <span data-preserver-spaces="true">Onto the </span><strong><span data-preserver-spaces="true">grouped bar charts </span></strong><span data-preserver-spaces="true">now. They display bars corresponding to a group next to each other instead of on top of each other. To use grouped bar charts, you need to put <code>position = position_dodge()</code> into a <code>geom_bar</code> layer:</span> <script src="https://gist.github.com/darioappsilon/1fd9b11c21dfc11c6968df5d2238b2d2.js"></script> <img class="size-full wp-image-6139" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b270bc9831078f4ea63ea1_13.webp" alt="Grouped bar chart (default)" width="1319" height="893" /> Image 13 - Grouped bar chart (default) <span data-preserver-spaces="true">You can change the coloring the same way you did with stacked bar charts - through the <code>scale_fill_manual</code> or <code>scale_fill_brewer</code> layers. Here's an example:</span> <script src="https://gist.github.com/darioappsilon/9df33cea82f30b3fbf89689cc5e45c3f.js"></script> <img class="size-full wp-image-6140" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e40a40386eec0ab8cb_bc6249a3_14.webp" alt="Grouped bar chart with custom colors" width="1319" height="893" /> Image 14 - Grouped bar chart with custom colors <span data-preserver-spaces="true">Finally, let's cover </span><strong><span data-preserver-spaces="true">horizontal bar charts</span></strong><span data-preserver-spaces="true">. They are useful when there are many categories on the x-axis or when their names are long. The <code>coord_flip()</code> is used to turn any vertical bar chart into a horizontal one:</span> <script src="https://gist.github.com/darioappsilon/20baf42ce63673b2125a6da295f2dee1.js"></script> <img class="size-full wp-image-6141" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e4c879fe9867fa566b_5e257a19_15.webp" alt="Horizontal bar chart (default)" width="1319" height="893" /> Image 15 - Horizontal bar chart (default) <span data-preserver-spaces="true">You can use the <code>scale_fill_manual</code> or <code>scale_fill_brewer</code> layers to change the color. Here's an example:</span> <script src="https://gist.github.com/darioappsilon/d9f9bd3e93b3f72f2ff64cc58dec4289.js"></script> <img class="size-full wp-image-6142" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e52cf9050b577789f2_7122bb85_16.webp" alt="Horizontal bar chart with custom colors" width="1319" height="893" /> Image 16 - Horizontal bar chart with custom colors <span data-preserver-spaces="true">Now you know how to make every type of bar chart - but there's still one thing you can improve. Let's see what that is in the next section.</span> <h2 id="labels"><span data-preserver-spaces="true">Add Labels to Individual Bars</span></h2> <span data-preserver-spaces="true">Bar charts can be hard to look at. Knowing the exact value is often a requirement. If the y-axis is on a scale of millions, reading values from a chart becomes an approximation (at best). That's where labels come in.</span> <span data-preserver-spaces="true">You can put text somewhere near the top of each bar to show the exact value. That solves the problem of reading values from the chart. It also makes it more user-friendly, as you don't have to divert your view to the y-axis constantly.</span> <span data-preserver-spaces="true">You'll learn how to put labels on top of bars. For the first example, you'll need to filter the dataset so only product A is shown. The reason is simple - <code>ggplot2</code> uses stacked bar charts by default, and there are two products in the stack for each quarter. You'll learn how to add labels for multiple stacks later, but let's start with the basics.</span> <span data-preserver-spaces="true">Here's the code:</span> <script src="https://gist.github.com/darioappsilon/2358c344adf75dea63edab7e1a658fde.js"></script> <img class="size-full wp-image-6143" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e6842ddef80cb869e6_aebd7cb0_17.webp" alt="Labels on top of bars" width="1319" height="893" /> Image 17 - Labels on top of bars <span data-preserver-spaces="true">But what if you want to </span><strong><span data-preserver-spaces="true">put the labels inside?</span></strong><span data-preserver-spaces="true"> Just play with <code>vjust</code> a bit. Setting it to 2 does the trick:</span> <script src="https://gist.github.com/darioappsilon/d0b6d350564165ad9cfff6984fc0f78c.js"></script> <img class="size-full wp-image-6144" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e6237e4636e90e2706_c512c466_18.webp" alt="Labels inside the bars" width="1319" height="893" /> Image 18 - Labels inside the bars <span data-preserver-spaces="true">Things get a bit trickier if you need labels for multiple stacks. You have to specify <code>position = position_stack()</code> inside the <code>geom_text</code> layer. Setting the <code>vjust</code> to 0.5 makes them centered:</span> <script src="https://gist.github.com/darioappsilon/fc54cf08eecbae94fac63ca42c616b7d.js"></script> <img class="size-full wp-image-6145" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e7d4f21bc5571cafe9_254e3a51_19.webp" alt="Labels inside the stacked bar chart" width="1319" height="893" /> Image 19 - Labels inside the stacked bar chart <span data-preserver-spaces="true">There's an alternative for a grouped bar chart. You'll have to specify <code>position = position_dodge()</code> for it to work. This code centers the labels inside every group:</span> <script src="https://gist.github.com/darioappsilon/dadd741745b51f8daa3b30407b560a9a.js"></script> <img class="size-full wp-image-6146" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b7d5e7237e4636e90e276c_05b04ab8_20.webp" alt="Labels centered inside the grouped bar chart" width="1319" height="893" /> Image 20 - Labels centered inside the grouped bar chart <span data-preserver-spaces="true">And that's all there is about labels and bar charts. There's still one section to cover, and that's adding lines to bar charts.</span> <h2 id="lines">Add Lines to ggplot2 Bar Charts</h2> Sometimes you want to add an extra touch to your bar charts. What you could do is add a line representing an average of all the bars. In our example, this would give you an insight into which products over which quarters performed better than average, and vice versa. It's a nice little touch-up and a bonus point when visualizing data, because the end-user has less thinking to do. You can add horizontal lines to ggoplot2 bar charts with the <code>geom_hline()</code> function. The example below adds a thick dashed black line representing the mean value of all profits: <script src="https://gist.github.com/darioappsilon/5b9533b4b4a472a03a8f04924764abaa.js"></script> <img class="size-full wp-image-12929" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b021863be1a225242296f5_Screenshot-2022-05-06-at-12.11.00.webp" alt="Image 21 - Adding horizontal lines to bar charts" width="2386" height="1780" /> Image 21 - Adding horizontal lines to bar charts Much easier to visually distinguish. Our imaginary company seems to have done better in the last two quarters. <h2><span data-preserver-spaces="true">Summary of ggplot2 Bar Charts</span></h2> <span data-preserver-spaces="true">Today you've learned how to make every type of bar chart in R and how to customize it with colors, titles, subtitles, and labels. You're now able to use ggplot2 bar charts for basic visualizations, reports, and dashboards. E</span><span data-preserver-spaces="true">xpect to see more basic R tutorials weekly (probably Sunday) and more advanced tutorials throughout the week. Fill out the subscribe form below so you never miss an update.</span> <blockquote><span data-preserver-spaces="true">Are you completely new to R? </span><a class="editor-rtfLink" href="https://wordpress.appsilon.com/r-for-programmers/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">Check out our detailed R guide for programmers.</span></a></blockquote> <span data-preserver-spaces="true">Learn how to include bar charts in interactive dashboards:</span> <ul><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/how-i-built-an-interactive-shiny-dashboard-in-2-days-without-any-experience-in-r/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How Our Project Leader Built Her First Shiny Dashboard with No R Experience</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/shiny-dashboard-ui-crash-course/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">A crash course in R Shiny UI</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/rapid-internationalization-of-shiny-apps-shiny-i18n-version-0-2/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to translate R Shiny dashboards</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/shiny-worker-package/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to make R Shiny faster</span></a></li><li><a class="editor-rtfLink" href="https://wordpress.appsilon.com/how-to-scale-a-shiny-dashboard/" target="_blank" rel="noopener noreferrer"><span data-preserver-spaces="true">How to scale R Shiny dashboards</span></a></li></ul>

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
r
data visualization
ggplot2
tutorials