Design and themes

Author

Jeffrey R. Stevens

Published

April 5, 2023

  1. Using the mtcars data, create a scatterplot of the fuel efficiency as a function of weight with color based on the number of cylinders.
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)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point()

  1. Repeat the scatterplot but with classic, bw, and dark themes.
mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme_classic()

mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme_bw()

mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme_dark()

  1. Repeat the scatterplot from #1 but with no minor grid lines and no legend.
mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme(panel.grid.minor = element_blank(),
        legend.position = "none")

  1. Repeat the scatterplot from #1 but no minor grid lines for the x-axis (keep them for the y-axis) and move the legend inside the plot area and remove the legend title.
mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme(panel.grid.minor.x = element_blank(),
        legend.position = c(0.8, 0.8),
        legend.title = element_blank())
Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2
3.5.0.
ℹ Please use the `legend.position.inside` argument of `theme()` instead.

  1. Repeat the scatterplot from #1 but make the major y-axis grid lines black dashed lines and change the text font to 12 point Times font.
mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme(panel.grid.major.y = element_line(color = "black", linetype = 2),
        text = element_text(family = "Times", size = 12))

  1. Create a version of scatterplot #1 that minimizes the data-ink ratio by reducing non-data-ink.
mtcars |> 
  mutate(cyl = as.factor(cyl)) |> 
  ggplot(aes(x = wt, y = mpg, color = cyl)) +
  geom_point() +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        legend.title = element_blank(),
        legend.position = c(0.8, 0.8))