Creating 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Creating 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.
Thanks for your feedback!