Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating Bar Plots | Data Visualization
Data Analysis with R

bookCreating Bar Plots

Why Use Bar Plots?

Bar plots are one of the most common ways to visualize categorical data. They are used to:

  • Compare counts or frequencies of categories;
  • Visualize group-wise summaries (like average price per fuel type);
  • Understand relationships between two categorical variables using grouped or stacked bars.

Whether you're showing the number of cars by fuel type or comparing transmission modes across fuels, bar plots make categorical comparisons clear and intuitive.

Bar Plot Syntax in ggplot2

A bar plot can be created in ggplot2 using geom_bar(). When only the x aesthetic is provided, geom_bar() automatically counts the number of observations in each category.

ggplot(data = df, aes(x = category)) +
  geom_bar()

If you also provide a y aesthetic with actual values, you must specify stat = "identity" so that ggplot2 uses the given values instead of counting rows.

ggplot(data = df, aes(x = category, y = value)) +
  geom_bar(stat = "identity")

Example: Count of Cars by Fuel Type

A bar plot can be used to show how many cars are available for each type of fuel. In this example, the bars are filled with light blue and outlined in red for emphasis. Labels are added for clarity, and theme_minimal() is applied for a cleaner look.

ggplot(df, aes(x = fuel)) + 
  geom_bar(fill = "lightblue", color = "red") +
  labs(title = "Car Distribution by Fuel Type", 
       x = "Fuel Type", 
       y = "Count") +
  theme_minimal()

This visualization highlights the distribution of cars across different fuel categories, making it easy to compare their relative availability.

question mark

What does geom_bar() do when only the x variable is provided?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 4

bookCreating Bar Plots

Swipe to show menu

Why Use Bar Plots?

Bar plots are one of the most common ways to visualize categorical data. They are used to:

  • Compare counts or frequencies of categories;
  • Visualize group-wise summaries (like average price per fuel type);
  • Understand relationships between two categorical variables using grouped or stacked bars.

Whether you're showing the number of cars by fuel type or comparing transmission modes across fuels, bar plots make categorical comparisons clear and intuitive.

Bar Plot Syntax in ggplot2

A bar plot can be created in ggplot2 using geom_bar(). When only the x aesthetic is provided, geom_bar() automatically counts the number of observations in each category.

ggplot(data = df, aes(x = category)) +
  geom_bar()

If you also provide a y aesthetic with actual values, you must specify stat = "identity" so that ggplot2 uses the given values instead of counting rows.

ggplot(data = df, aes(x = category, y = value)) +
  geom_bar(stat = "identity")

Example: Count of Cars by Fuel Type

A bar plot can be used to show how many cars are available for each type of fuel. In this example, the bars are filled with light blue and outlined in red for emphasis. Labels are added for clarity, and theme_minimal() is applied for a cleaner look.

ggplot(df, aes(x = fuel)) + 
  geom_bar(fill = "lightblue", color = "red") +
  labs(title = "Car Distribution by Fuel Type", 
       x = "Fuel Type", 
       y = "Count") +
  theme_minimal()

This visualization highlights the distribution of cars across different fuel categories, making it easy to compare their relative availability.

question mark

What does geom_bar() do when only the x variable is provided?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt