Plotting amounts: barcharts and dot plots

Author

Jeffrey R. Stevens

Published

April 14, 2023

  1. Using the mtcars data, create a barchart of the counts for each level of cylinder.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mtcars |> 
  mutate(cyl = as.factor(cyl),
         gear = as.factor(gear)) |> 
  ggplot(aes(x = cyl)) +
  geom_bar()

  1. Repeat the barchart but stack the counts by gear.
mtcars |> 
  mutate(cyl = as.factor(cyl),
         gear = as.factor(gear)) |> 
  ggplot(aes(x = cyl, fill = gear)) +
  geom_bar()

  1. Recreate this plot:

mtcars |>
  mutate(cyl = as.factor(cyl),
         cyl = str_c(cyl, " cylinders"),
         gear = as.factor(gear),
         gear = str_c(gear, " gears")) |>
  ggplot(aes(x = cyl, fill = gear)) +
  geom_bar(position = "fill")

  1. Repeat barchart #2 but set the position to “dodge”.
mtcars |> 
  mutate(cyl = as.factor(cyl),
         gear = as.factor(gear)) |> 
  ggplot(aes(x = cyl, fill = gear)) +
  geom_bar(position = "dodge")

  1. Whoa, what happened to 8 cylinders? Unfortunately, since there were only two levels of gear for 8 cylinders, it just split the bars in two. To hold the numbers of bars the same across all levels, you can set position to position_dodge(preserve = "single"). Try that.
mtcars |> 
  mutate(cyl = as.factor(cyl),
         gear = as.factor(gear)) |> 
  ggplot(aes(x = cyl, fill = gear)) +
  geom_bar(position = position_dodge(preserve = "single"))

  1. Well, that’s better—the two bars are the same width as all of the other bars. But the 4 gears should show up as 0. To fix, we need to count the data first, find implicitly missing data, and plot using geom_col(). So first, find counts for the combinations of cylinders and gears. Then use complete() to find the implicitly missing combinations. Then replace the NAs with 0s. Then use geom_col() to plot these values with the position dodged.
mtcars |> 
  mutate(cyl = as.factor(cyl),
         gear = as.factor(gear)) |> 
  count(cyl, gear) |> 
  complete(cyl, gear) |> 
  mutate(n = as.numeric(str_replace_na(n, "0"))) |> 
  ggplot(aes(x = cyl, y = n, fill = gear)) +
  geom_col(position = position_dodge())

  1. Make a dotplot of the counts for each level of carb and plot carb on the y-axis and the count on the x-axis. Reminder that first you’ll need to count the observations in each level of carb before starting the plot.
mtcars |> 
  count(carb) |> 
  ggplot(aes(x = carb, y = n)) +
  geom_point() +
  coord_flip()

  1. Repeat dotplot #8 but order carb based on the counts from highest to lowest count.
mtcars |> 
  count(carb) |> 
  ggplot(aes(x = fct_reorder(as.factor(carb), n), y = n)) +
  geom_point() +
  coord_flip()