📖 Scenario: You are working with a small store's sales data. You want to visualize how many items were sold for each product category using bar plots.
🎯 Goal: Create a bar plot using geom_bar and another bar plot using geom_col in R with ggplot2 to show the number of items sold per product category.
📋 What You'll Learn
Create a data frame called sales_data with columns category and items_sold with exact values
Create a variable called bar_plot_geom_bar that uses ggplot with geom_bar(stat = "count") to plot counts of categories
Create a variable called bar_plot_geom_col that uses ggplot with geom_col() to plot items_sold per category
Print both plots using print()
💡 Why This Matters
🌍 Real World
Bar plots are commonly used in business to quickly see how different categories compare in counts or values, such as sales per product type.
💼 Career
Data analysts and data scientists use bar plots to communicate insights clearly to teams and stakeholders.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales_data with two columns: category containing the values 'Fruits', 'Vegetables', 'Dairy', 'Bakery', and items_sold containing the values 50, 30, 20, 40 respectively.
R Programming
Hint
Use data.frame() with category = c(...) and items_sold = c(...) to create the data frame.
2
Create bar plot using geom_bar
Create a variable called bar_plot_geom_bar that uses ggplot with sales_data as data, mapping category to the x-axis, and add geom_bar(stat = "count") to plot the count of each category.
R Programming
Hint
Use ggplot(sales_data, aes(x = category)) + geom_bar(stat = "count") to create the bar plot.
3
Create bar plot using geom_col
Create a variable called bar_plot_geom_col that uses ggplot with sales_data as data, mapping category to the x-axis and items_sold to the y-axis, and add geom_col() to plot the exact items sold per category.
R Programming
Hint
Use ggplot(sales_data, aes(x = category, y = items_sold)) + geom_col() to create the bar plot with exact values.
4
Print both bar plots
Use print() to display both bar_plot_geom_bar and bar_plot_geom_col plots.
R Programming
Hint
Use print(bar_plot_geom_bar) and print(bar_plot_geom_col) to show the plots.
Practice
(1/5)
1. What is the main difference between geom_bar() and geom_col() in R's ggplot2 package?
easy
A. geom_bar() counts data automatically; geom_col() uses provided values for bar heights.
B. geom_bar() creates line plots; geom_col() creates bar plots.
C. geom_bar() requires numeric x-axis; geom_col() requires categorical x-axis.
D. geom_bar() is for pie charts; geom_col() is for bar charts.
Solution
Step 1: Understand geom_bar() behavior
geom_bar() automatically counts the number of occurrences for each category on the x-axis and plots bars based on these counts.
Step 2: Understand geom_col() behavior
geom_col() uses the y-values provided in the data directly as the height of the bars, without counting.
Final Answer:
geom_bar() counts data automatically; geom_col() uses provided values for bar heights. -> Option A
Quick Check:
Counting vs direct values = A [OK]
Hint: Remember: bar counts vs bar heights from data [OK]
Common Mistakes:
Thinking both functions count data automatically
Confusing bar plots with line or pie charts
Assuming geom_col() counts data
2. Which of the following is the correct syntax to create a bar plot using geom_col() with ggplot2 in R?
easy
A. ggplot(data, aes(x=category)) + geom_col()
B. ggplot(data, aes(x=category, y=value)) + geom_col()
C. ggplot(data, aes(y=value)) + geom_col()
D. ggplot(data, aes(x=value, y=category)) + geom_col()
Solution
Step 1: Identify required aesthetics for geom_col()
geom_col() needs both x (categorical) and y (numeric) aesthetics to set bar positions and heights.
Step 2: Check each option's aesthetics
ggplot(data, aes(x=category, y=value)) + geom_col() correctly maps x to category and y to value. Options A and C miss one aesthetic, and D swaps x and y incorrectly.
Final Answer:
ggplot(data, aes(x=category, y=value)) + geom_col() -> Option B
Quick Check:
Both x and y needed for geom_col() = B [OK]
Hint: geom_col() needs both x and y aesthetics [OK]
Common Mistakes:
Omitting y aesthetic for geom_col()
Swapping x and y aesthetics
Using geom_col() without specifying y
3. Given the following R code, what will be the height of the bars in the plot?