top of page
Writer's pictureBernard Kilonzo

How to Make Bar-in-Bar Chart in R

image of a computer screen with code written on it

Introduction

bar-in-bar chart is a specialized type of bar chart that displays two interlocking bar charts, allowing for a visual comparison between two related measures. This format is particularly useful for illustrating changes between two points, such as profits over different timelines or comparisons across various categories.

A Step-by-Step Guide on How to Create a Bar-in-Bar Chart in R

To create a bar-in-bar chart in R using ggplot2, first you need to load the necessary packages in your R session as shown below.

r code on loading necessary packages

Next, set the working directory and load the dataset. In this case, I will be using the Sample – Superstore dataset.

r code on setting working directory and loading dataset

With data loaded, let’s create a bar-in-bar chart comparing Sales and Profit for different Regions using the code below.

r code on creating a bar-in-bar chart using ggplot2

Note:  In the above code, I have summarized the Sales and Profit values using SUM. And applied different colors and width for each bar.

Executing the above code results in.

a basic bar-in-bar chart created in r using ggplot2 package

Let’s customize the view by applying different colors and use the classic theme to declutter the view.

customizing bar-in-bar in r

Executing the above code results in.

final output of a bar-in-bar chart created in r using the ggplot2 package

Conclusion

Bar-in-bar charts are an effective visualization tool for comparing related data sets, particularly when demonstrating changes or differences over time or categories. Their design enhances clarity and allows for immediate visual interpretation, making them a popular choice in data analysis and reporting contexts.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Blog.png
bottom of page