Week 05: Visualize

[05a] Graphics / Scatterplots

Topics

  • Learn the principles that govern data visualization
  • Create and save scatterplots using ggplot() and ggsave()

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Scatterplots

Create a new scatterplot using the mpg dataset (from the {ggplot2} package).

  • Map Highway MPG (hwy) to the x-axis.
  • Map City MPG (cty) to the y-axis.
  • Ensure the points are displayed as geometric objects.

Answer key

library(tidyverse)
data("mpg", package = "ggplot2")
p <- 
  ggplot(
    data = mpg,
    mapping = aes(x = hwy, y = cty)
  ) +
  geom_point()
p

2) Saving Graphics

Using ggsave(), perform the following steps:

  1. Save the plot from question 1 as a PNG file named “plot_mpg.png” with a width of 8 inches and a height of 6 inches.
  2. Save the plot from question 1 as a PDF file named “plot_mpg.pdf” with the same dimensions.
  3. Open both files on your computer. Zoom in closely on the points in both files and describe the difference in quality between the raster (PNG) and vector (PDF) versions.

Answer key

Part (a)

ggsave(
  filename = "plot_mpg.png",
  plot = p,
  width = 8,
  height = 6,
  units = "in"
)

Part (b)

ggsave(
  filename = "plot_mpg.pdf",
  plot = p,
  width = 8,
  height = 6,
  units = "in"
)

[05b] Layers / Aesthetics

Topics

  • Combine layers of overlapping geometric objects
  • Learn aesthetic setting, mapping, and grouping

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Layers and Settings

  1. Layering Geoms: The pressure dataset (from the “datasets” package) contains data on the relationship between temperature and vapor pressure of mercury. Create a plot to visualize this.
    • Map Temperature (temperature) to the x-axis and Pressure (pressure) to the y-axis.
    • Add a layer of points using geom_point().
    • Add a layer of lines using geom_line().
  2. Setting Aesthetics: Modify the plot from Part (a) to change the static appearance.
    • Keep the points layer, but set the size of the points to 3.
    • Modify the line layer: set the color to “blue” and the linetype to “dashed”.

Answer key

Part (a)

library(tidyverse)
data("pressure", package = "datasets")

pressure |>
  ggplot(aes(x = temperature, y = pressure)) +
  geom_point() +
  geom_line()

Part (b)

pressure |>
  ggplot(aes(x = temperature, y = pressure)) +
  geom_point(size = 3) +
  geom_line(color = "blue", linetype = "dashed")

2) Mappings and Grouping

  1. Mapping Aesthetics: Create a scatterplot using the mpg dataset (from the “ggplot2” package) to see if fuel efficiency data is clustered by cylinder count.
    • Map City MPG (cty) to the x-axis and Highway MPG (hwy) to the y-axis.
    • Map Cylinders (cyl) to the color aesthetic. (Note: You may want to wrap cyl in factor() so R treats it as categories rather than a continuous number).
    • Add a layer of points and set their size to 3.
  2. Explicit Grouping: The Orange dataset (from the “datasets” package) tracks the growth of 5 specific orange trees. Create a line plot to visualize this growth.
    • Map Age (age) to the x-axis and Circumference (circumference) to the y-axis.
    • Map Tree (Tree) to the group aesthetic.
    • Add a layer of lines using geom_line().
    • Bonus: What happens if you remove the group aesthetic?

Answer key

Part (a)

data("mpg", package = "ggplot2")

mpg |>
  ggplot(aes(x = cty, y = hwy, color = factor(cyl))) +
  geom_point(size = 3)

Part (b)

data("Orange", package = "datasets")

Orange |>
  ggplot(aes(x = age, y = circumference, group = Tree)) +
  geom_line()

[05c] Distributions / Relationships

Topics

  • Visualize the distribution of a single variable
  • Visualize relationships between several variables

Readings

Slides

*Note that you can click the three-line (hamburger) icon on the bottom-left of the slides to access a navigation menu. You can click inside the slides region and press the left and right arrow keys on your keyboard to advance and reverse the slides and animations.

Practice

1) Distributions

  1. Create a histogram using the mpg dataset (from the {ggplot2} package) to visualize the distribution of City MPG.
    • Map City MPG (cty) to the x-axis.
    • Ensure the bars are displayed as geometric objects using geom_histogram().
    • Set the binwidth argument to 2.
  2. Create a bar chart using the mpg dataset to visualize the distribution of Vehicle Class.
    • Map Vehicle Class (class) to the x-axis.
    • Ensure the bars are displayed as geometric objects using geom_bar().

Answer key

Part (a)

library(tidyverse)
data("mpg", package = "ggplot2")

mpg |>
  ggplot(aes(x = cty)) +
  geom_histogram(binwidth = 2)

Part (b)

mpg |>
  ggplot(aes(x = class)) +
  geom_bar()

2) Relationships

  1. Create a boxplot using the mpg dataset to visualize the relationship between vehicle class and Highway MPG.
    • Map Vehicle Class (class) to the x-axis.
    • Map Highway MPG (hwy) to the y-axis.
    • Ensure the distribution summaries are displayed as geometric objects using geom_boxplot().
  2. Create a stacked bar chart using the mpg dataset to visualize the relationship between vehicle class and drive train.
    • Map Vehicle Class (class) to the x-axis.
    • Map Drive Train (drv) to the fill aesthetic.
    • Ensure the bars are displayed as geometric objects using geom_bar().

Answer key

Part (a)

mpg |>
  ggplot(aes(x = class, y = hwy)) +
  geom_boxplot()

Part (b)

mpg |>
  ggplot(aes(x = class, fill = drv)) +
  geom_bar()