Use the bar chart when you want to compare different categories on some numerical metric.
Visualizing frequencies or counts
Visualizing categorical data
Code (basic R bar chart)
# Load ggplot2 graphics package library(ggplot2) # Create dummy dataset df.dummy_data <- data.frame(category_var = c("A","B","C","D","E"), numeric_var = c(5,2,9,4,5) ) # Plot dataset with ggplot2 ggplot(data=df.dummy_data, aes(x=category_var, y=numeric_var)) + geom_bar(stat="identity")
Essentially, we create a plot using ggplot2 in a few key steps:
1. Specify the dataset
2. Create a relationship between the dataset and the “aesthetic attributes” of the things we want to plot
3. Plot stuff
Let’s take a look at this line by line.
(Note: we’re not going to review the dataset creation process in this tutorial. We’ll cover that in a separate post.)
ggplot(data=df.dummy_data, aes(x=category_var, y=numeric_var))
Here in the first line, we’re taking care of the first two steps: specifying the data we’re going to plot and creating a relationship between the dataset and “aesthetic elements.”
Next, we call the
Next we specify what we want to plot with the following code:
To be clear, this is the code that actually does the plotting.
Note as well that inside of
Side note: R vs Excel
As an aside, you might be thinking that this doesn’t do much that you can’t do in Excel. While I generally agree with that in the simplest of cases, there are a lot of advantages to building such a bar chart in R, such as greater workflow automation, use the
Filled Bar (AKA, 100% Bar)