0
0
R Programmingprogramming~5 mins

Bar plots (geom_bar, geom_col) in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main difference between geom_bar() and geom_col() in ggplot2?

geom_bar() counts the number of cases at each x value and plots the height automatically.

geom_col() uses the y values you provide directly to set the height of the bars.

Click to reveal answer
beginner
How does geom_bar() behave if you provide a y aesthetic?

geom_bar() ignores the y aesthetic because it is designed to count observations. To plot heights from y values, use geom_col().

Click to reveal answer
beginner
Write a simple example of geom_bar() to plot counts of a categorical variable category in a data frame df.
ggplot(df, aes(x = category)) + geom_bar()

This will count how many times each category appears and draw bars accordingly.

Click to reveal answer
beginner
Write a simple example of geom_col() to plot values from a data frame df with columns category and value.
ggplot(df, aes(x = category, y = value)) + geom_col()

This will draw bars with heights equal to the value for each category.

Click to reveal answer
beginner
Why might you choose geom_col() over geom_bar()?

Use geom_col() when you already have summarized data with values you want to plot as bar heights. geom_bar() is for raw data where you want to count occurrences.

Click to reveal answer
Which function automatically counts the number of observations for each x value in ggplot2?
Ageom_bar()
Bgeom_col()
Cgeom_point()
Dgeom_line()
If you have a data frame with categories and their sales numbers, which geom should you use to plot sales as bar heights?
Ageom_bar()
Bgeom_col()
Cgeom_histogram()
Dgeom_boxplot()
What happens if you provide a y aesthetic to geom_bar()?
AIt uses y to set bar heights
BIt plots a line instead
CIt ignores y and counts observations
DIt throws an error
Which ggplot2 function would you use to plot the frequency of a categorical variable?
Ageom_text()
Bgeom_col()
Cgeom_point()
Dgeom_bar()
You want to plot a bar chart from summarized data with values already calculated. Which is best?
Ageom_col()
Bgeom_histogram()
Cgeom_bar()
Dgeom_area()
Explain the difference between geom_bar() and geom_col() and when to use each.
Think about whether you have raw data or summarized values.
You got /4 concepts.
    Describe how you would create a bar plot showing the number of items in each category using ggplot2.
    Counting categories is automatic with geom_bar.
    You got /3 concepts.