Bird
Raised Fist0
R Programmingprogramming~10 mins

Bar plots (geom_bar, geom_col) in R Programming - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic bar plot using ggplot2 with geom_bar.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15))
ggplot(data, aes(x = category)) + geom_bar(stat = [1])
Drag options to blanks, or click blank then click option'
A"identity"
B"mean"
C"sum"
D"count"
Attempts:
3 left
💡 Hint
Common Mistakes
Using stat = "identity" when data does not have y values.
2fill in blank
medium

Complete the code to create a bar plot using geom_col with the height of bars from the count column.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(10, 20, 15))
ggplot(data, aes(x = category, y = [1])) + geom_col()
Drag options to blanks, or click blank then click option'
Acount
Bcategory
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Mapping x to y aesthetic instead of count.
3fill in blank
hard

Fix the error in the code to correctly plot counts of categories using geom_bar.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'A', 'C', 'B', 'B'))
ggplot(data, aes(x = [1])) + geom_bar()
Drag options to blanks, or click blank then click option'
Acount
Bcategory
Cvalue
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using stat = "identity" without y aesthetic.
4fill in blank
hard

Fill both blanks to create a bar plot with bars colored by category and width set to 0.5.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), count = c(5, 10, 7))
ggplot(data, aes(x = category, y = count, fill = [1])) + geom_col(width = [2])
Drag options to blanks, or click blank then click option'
Acategory
Bcount
C0.5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using count for fill aesthetic.
Setting width to 1 when asked for 0.5.
5fill in blank
hard

Fill all three blanks to create a stacked bar plot showing counts by category and subgroup.

R Programming
library(ggplot2)
data <- data.frame(category = c('A', 'A', 'B', 'B'), subgroup = c('X', 'Y', 'X', 'Y'), count = c(3, 2, 5, 4))
ggplot(data, aes(x = [1], y = [2], fill = [3])) + geom_col()
Drag options to blanks, or click blank then click option'
Acategory
Bcount
Csubgroup
Dgroup
Attempts:
3 left
💡 Hint
Common Mistakes
Using group instead of subgroup for fill.
Mapping y to subgroup instead of count.

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

  1. 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.
  2. 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.
  3. Final Answer:

    geom_bar() counts data automatically; geom_col() uses provided values for bar heights. -> Option A
  4. 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

  1. Step 1: Identify required aesthetics for geom_col()

    geom_col() needs both x (categorical) and y (numeric) aesthetics to set bar positions and heights.
  2. 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.
  3. Final Answer:

    ggplot(data, aes(x=category, y=value)) + geom_col() -> Option B
  4. 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?
library(ggplot2)
data <- data.frame(fruit = c('apple', 'banana', 'apple', 'orange', 'banana', 'banana'))
ggplot(data, aes(x=fruit)) + geom_bar()
medium
A. Bars show counts: apple=2, banana=3, orange=1
B. Bars show values from a y column (not provided)
C. Bars will be empty because y is missing
D. Bars show sum of fruit names alphabetically

Solution

  1. Step 1: Understand geom_bar() default behavior

    Without a y aesthetic, geom_bar() counts the number of rows for each x category.
  2. Step 2: Count occurrences in data

    apple appears 2 times, banana 3 times, orange 1 time.
  3. Final Answer:

    Bars show counts: apple=2, banana=3, orange=1 -> Option A
  4. Quick Check:

    geom_bar() counts rows = A [OK]
Hint: geom_bar() counts rows if y not given [OK]
Common Mistakes:
  • Expecting geom_bar() to use y values without specifying
  • Thinking bars will be empty without y
  • Confusing counts with sums or other calculations
4. Consider this R code snippet:
library(ggplot2)
data <- data.frame(category = c('A', 'B', 'C'), value = c(5, 3, 8))
ggplot(data, aes(x=category)) + geom_bar(stat='identity')

What is the problem with this code?
medium
A. The geom_bar() function does not accept stat argument.
B. The data frame has wrong column names.
C. The x aesthetic should be numeric, not categorical.
D. Using stat='identity' with geom_bar() requires y aesthetic but it's missing.

Solution

  1. Step 1: Understand stat='identity' in geom_bar()

    When using stat='identity', geom_bar() expects a y aesthetic to set bar heights directly.
  2. Step 2: Check the code's aesthetics

    The code only maps x=category but does not map y=value, so bar heights are undefined.
  3. Final Answer:

    Using stat='identity' with geom_bar() requires y aesthetic but it's missing. -> Option D
  4. Quick Check:

    stat='identity' needs y aesthetic = C [OK]
Hint: Use y aesthetic with stat='identity' in geom_bar() [OK]
Common Mistakes:
  • Forgetting to add y aesthetic with stat='identity'
  • Assuming geom_bar() always counts data
  • Thinking stat argument is invalid
5. You have a data frame with categories and their sales numbers:
sales_data <- data.frame(
  product = c('Pen', 'Pencil', 'Eraser', 'Pen', 'Pencil'),
  sales = c(10, 5, 8, 7, 3)
)

You want to create a bar plot showing total sales per product. Which code correctly creates this plot?
hard
A. ggplot(sales_data, aes(x=product, y=sales)) + geom_bar()
B. ggplot(sales_data, aes(x=product)) + geom_col()
C. ggplot(sales_data, aes(x=product, y=sales)) + geom_col()
D. ggplot(sales_data, aes(x=product)) + geom_bar()

Solution

  1. Step 1: Understand data and goal

    The data has multiple rows per product. Goal is bars with heights = sum(sales) per product (Pen=17, Pencil=8, Eraser=8).
  2. Step 2: Choose correct plotting method

    geom_col(aes(x=product, y=sales)) uses stat='identity' and sums y values per x category for bar heights.
  3. Step 3: Check options

    A: correct, sums sales. B: no y, invalid. C: geom_bar() defaults to stat='count', ignores y, counts rows (2,2,1). D: counts rows (2,2,1).
  4. Final Answer:

    ggplot(sales_data, aes(x=product, y=sales)) + geom_col() -> Option C
  5. Quick Check:

    geom_col() sums y per x = A [OK]
Hint: geom_col(aes(x,y)) sums y per category [OK]
Common Mistakes:
  • Using geom_bar() (default count) when needing sum of y
  • Omitting y aesthetic with geom_col()
  • Thinking manual aggregation is always required
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard