Bar Plot

Sample Dataset

Exercises

Create a basic bar plot of category using geom_bar().

Solution:

data |>  
ggplot() +
  aes(x = category) +
  geom_bar() +
  labs(title = "Count of Each Category", 
           x = "Category", 
           y = "Count")

Create a stacked bar plot of category by subgroup, showing how each subgroup contributes to the total count in each category.

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar() +
  labs(title = "Stacked Bar Plot by Category and Subgroup", 
           x = "Category", 
           y = "Count", 
           fill = "Subgroup")

Create a grouped bar plot of category by subgroup, so that each subgroup is displayed as separate bars within each category.

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar(position = "dodge") +
  labs(title = "Grouped Bar Plot by Category and Subgroup", 
           x = "Category", 
           y = "Count", 
           fill = "Subgroup")

Create a bar plot of category and change the fill color to “steelblue”.

Solution:

data |> 
  ggplot()  +
  aes(x = category) +
  geom_bar(fill = "steelblue") +
  labs(title = "Bar Plot of Category with Steelblue Fill", 
           x = "Category", 
           y = "Count")

Create a bar plot of category with bars filled according to subgroup (e.g., “X” = red, “Y” = green).

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar(position = "dodge") +
  scale_fill_manual(values = c("X" = "red", 
                               "Y" = "green")) +
  labs(title = "Grouped Bar Plot with Custom Colors by Subgroup", 
           x = "Category", 
           y = "Count", 
        fill = "Subgroup")

Create a bar plot of category and use facet_grid to split the bar plot by region.

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar() +
  facet_grid(. ~ region) +
  labs(title = "Bar Plot of Category Faceted by Region", 
           x = "Category", 
           y = "Count", 
        fill = "Subgroup")

Create a bar plot of category with subgroup fill, add labels to each bar, and rotate the x-axis labels to make them more readable.

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar(position = "dodge") +
  labs(title = "Grouped Bar Plot with Rotated Labels", 
           x = "Category", 
           y = "Count", 
        fill = "Subgroup") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Create a bar plot of category and apply a different theme (e.g., theme_bw, theme_minimal, etc.).

Solution:

data |> 
  ggplot() +
  aes(x = category) +
  geom_bar(fill = "purple") +
  theme_minimal() +
  labs(title = "Bar Plot of Category with Minimal Theme", 
           x = "Category", 
           y = "Count")

Create a bar plot of category with bars ordered by frequency, showing the most frequent categories first.

Solution:

data |> 
ggplot() +
  aes(x = reorder(category, -table(category)[category])) +
  geom_bar(fill = "orange") +
  labs(title = "Bar Plot of Category Ordered by Frequency", 
           x = "Category", 
           y = "Count")

Create a grouped bar plot of category by subgroup and set the width of the bars to 0.6.

Solution:

data |> 
  ggplot() +
  aes(x = category, fill = subgroup) +
  geom_bar(position = "dodge", width = 0.6) +
  labs(title = "Grouped Bar Plot with Custom Bar Width", 
           x = "Category", 
           y = "Count", 
        fill = "Subgroup")

Create a bar plot that bins the score variable into intervals (e.g., 50-60, 60-70, etc.) and displays the count for each interval.

Hint: Use the cut() function to create intervals and plot the counts of each interval.

Solution:

data |> 
  mutate(score_bin = cut(score, 
                         breaks = seq(50, 100, by = 10), 
                         include.lowest = TRUE)) |> 
  ggplot() +
    aes(x = score_bin) +
    geom_bar(fill = "skyblue") +
    labs(title = "Binned Score Distribution", 
             x = "Score Interval", 
             y = "Count") +
    theme_minimal()

Calculate the average score for each category and display it as a bar plot.

Hint: Use dplyr::group_by() and dplyr::summarize() to calculate the average score, then use geom_bar(stat = "identity") to plot the results.

Solution:

data |> 
  group_by(category) |> 
  summarize(avg_score = mean(score)) |> 
  ggplot() +
    aes(x = category, y = avg_score, fill = category) +
    geom_bar(stat = "identity", color = "black") +
    labs(title = "Average Score by Category", 
            x = "Category", 
            y = "Average Score") +
    theme_minimal()

Create a stacked bar plot showing the count of score intervals for each region.

Hint: Use cut() to bin score, then plot the counts for each region as stacked bars.

Solution:

data |> 
  mutate(score_bin = cut(score, 
                        breaks = seq(50, 100, by = 10),
                        include.lowest = TRUE)) |> 
  ggplot() +
    aes(x = region, fill = score_bin) +
    geom_bar() +
    labs(title = "Stacked Bar Plot of Score Bins by Region", 
             x = "Region", 
             y = "Count", 
          fill = "Score Interval") +
    theme_minimal()