Bar Plots

Get introduced to bar charts and learn how to modify them.

Features

Bar plots are a type of graphical display used to represent categorical data. They consist of a series of bars, each representing a category, with the height or length of the bar proportional to the value or frequency of the category.

Press + to interact
Bar plot
Bar plot

We typically use bar plots to compare the values of different categories or to show how the values of a single category vary across different subgroups. They are commonly used in fields such as business, economics, and social sciences. For example, we can use a bar chart to represent the demand for different car brands in a population.

Bar chart with barplot()

We use the barplot() function to generate bar plots in R. Although it can take optional arguments for configuration, we can generate a bar chart just by supplying the dataset variable to the function. The function creates a bar object for each data point. The bar sizes are proportionate to the values.

# Syntax structure
barplot(<data>)

Here is a barplot visualization of randomly generated data:

Press + to interact
# We use a custom random dataset in this exercise
data <- sample(1:20,5) # Generate a dataset from random numbers
print(data) # Print the dataset
barplot(data) # Visualize the dataset
  • Line 2: We create a dataset from five random numbers. The dataset includes 5, 15, 10, 2, and 9.

  • Line 4: We generate a simple barplot ...