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

Estimated time:

<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="" 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=""></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=""></script> <span data-preserver-spaces="true">Here's the corresponding visualization:</span> <img class="size-full wp-image-6126" src="" 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=""></script> <img class="size-full wp-image-6127" src="" 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=""></script> <img class="size-full wp-image-6128" src="" 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=""></script> <div class="mceTemp"></div> <img class="size-full wp-image-6129" src="" 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="" 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=""></script> <img class="size-full wp-image-6130" src="" 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=""></script> <img class="size-full wp-image-6131" src="" 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=""></script> <img class="size-full wp-image-6132" src="" 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=""></script> <img class="size-full wp-image-6133" src="" 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=""></script> <img class="size-full wp-image-6134" src="" 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=""></script> <img class="size-full wp-image-6135" src="" 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=""></script> <img class="size-full wp-image-6137" src="" 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="" 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=""></script> <img class="size-full wp-image-6138" src="" 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=""></script> <img class="size-full wp-image-6139" src="" 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=""></script> <img class="size-full wp-image-6140" src="" 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=""></script> <img class="size-full wp-image-6141" src="" 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=""></script> <img class="size-full wp-image-6142" src="" 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=""></script> <img class="size-full wp-image-6143" src="" 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=""></script> <img class="size-full wp-image-6144" src="" 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=""></script> <img class="size-full wp-image-6145" src="" 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=""></script> <img class="size-full wp-image-6146" src="" 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=""></script> <img class="size-full wp-image-12929" src="" 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="" 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="" 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="" 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="" 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="" 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="" 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.
Have questions or insights?
Engage with experts, share ideas and take your data journey to the next level!
Join Slack

Take Your Business Further with Custom
Data Solutions

Unlock the full potential of your enterprise with our data services, tailored to the unique needs of Fortune 500 companies. Elevate your strategy—connect with us today!

data visualization