This document is a record of some basic ggplot2 exercises.

library(tidyverse)
library(gapminder)

# Put the data in the Environment
gapminder <- gapminder::gapminder

# Extract the data for the latest year in the dataset, for plots that don't involve year.
gapminder_latest <- filter(gapminder, year == 2007)

🚧 After running the setup code chunk, you should have two tibbles, both listed in the “Environment” tab. Name and describe those tibbles here. You may know more about their value when ’re finished with the lab, so write what you can now and come back later and fill in the details.

Styling and labeling plots

🚧 Imagine that you’re trying to understand the life expectancy in the US over time, and modify the following plot to:

  1. Add labels (labs) for the x and y axes. Use ?gapminder at the Console to get the data documentation if needed.
  2. Add a title (also labs).
  3. Set the color of the line to green. (Is it green now?)
  4. Set the chunk name to something more meaningful than plot-1.

Finally, add a short explanation of what the visualization suggests.

gapminder %>% 
  filter(country == "United States") %>% 
  ggplot() +
  aes(x = year, y = lifeExp) +
  geom_line()

Mapping vs Faceting

🚧 In the following plot, notice that we’re filtering to only include rows where the country is in the list of countries that we made. This is so that we don’t have 142 different colors or facets in these graphs.

Imagine that you’re trying to understand the life expectancy in these countries over time, and then do the following:

  1. Run the plot as specified. Why is it shaped like this? (If you’re not sure, click gapminder in the Environment pane and think about how you’d draw a line with that data.)
  2. Map color to country. How has the plot changed?
  3. Facet by country. Is this better than the color mapping?
  4. Setting scales = "free_y" in the faceting function. Is this better?

When finished, set your plot to the most useful form given the goal stated above. Add an explanation of what the visualization suggests.

countries <- c(
  "China", "India", "United States",
  "Indonesia", "Brazil", "Pakistan",
  "Bangladesh", "Nigeria", "Japan")

gapminder %>% 
  filter(country %in% countries) %>% 
  ggplot() +
  aes(x = year, y = lifeExp) +
  geom_line()

Univariate analysis — Continuous

🚧 Pick one of the continuous variables and pose a question you’d like to answer. Then, make a plot that shows its distribution and help answer your question.

Include your plot and a description of what it suggests with respect to your initial question.

Univariate Analysis — Categorical

🚧 Imagine that you’d like to know which continent has the most countries. Compose a plot that helps to answer this question. (Use gapminder_latest, which has one row per country.)

Include your plot and a description of what it suggests with respect to the given question.

Bivariate Analysis — Numerical/Categorical

🚧 Pick one of the continuous variables and set a question you’d like to answer about that variable with respect to the continents. If possible, choose a variable you haven’t chosen before. Compose a plot that helps you answer your question.

Include your plot and a description of what it suggests with respect to your initial question.

LS0tCnRpdGxlOiAiTGFiIDQuMSAtIFBsb3R0aW5nIHdpdGggYGdncGxvdDJgIgphdXRob3I6ICJBdXRob3IgR29lcyBIZXJlIgpkYXRlOiAiU2VtZXN0ZXIgR29lcyBIZXJlIgpvdXRwdXQ6CiAgaHRtbF9kb2N1bWVudDoKICAgIGNvZGVfZG93bmxvYWQ6IHRydWUKLS0tCgpUaGlzIGRvY3VtZW50IGlzIGEgcmVjb3JkIG9mIHNvbWUgYmFzaWMgZ2dwbG90MiBleGVyY2lzZXMuCgpgYGB7ciBzZXR1cCwgbWVzc2FnZT1GQUxTRX0KbGlicmFyeSh0aWR5dmVyc2UpCmxpYnJhcnkoZ2FwbWluZGVyKQoKIyBQdXQgdGhlIGRhdGEgaW4gdGhlIEVudmlyb25tZW50CmdhcG1pbmRlciA8LSBnYXBtaW5kZXI6OmdhcG1pbmRlcgoKIyBFeHRyYWN0IHRoZSBkYXRhIGZvciB0aGUgbGF0ZXN0IHllYXIgaW4gdGhlIGRhdGFzZXQsIGZvciBwbG90cyB0aGF0IGRvbid0IGludm9sdmUgeWVhci4KZ2FwbWluZGVyX2xhdGVzdCA8LSBmaWx0ZXIoZ2FwbWluZGVyLCB5ZWFyID09IDIwMDcpCmBgYAoK8J+apwpBZnRlciBydW5uaW5nIHRoZSBgc2V0dXBgIGNvZGUgY2h1bmssIHlvdSBzaG91bGQgaGF2ZSB0d28gdGliYmxlcywgYm90aCBsaXN0ZWQgaW4gdGhlICJFbnZpcm9ubWVudCIgdGFiLiBOYW1lIGFuZCBkZXNjcmliZSB0aG9zZSB0aWJibGVzIGhlcmUuIFlvdSBtYXkga25vdyBtb3JlIGFib3V0IHRoZWlyIHZhbHVlIHdoZW4gJ3JlIGZpbmlzaGVkIHdpdGggdGhlIGxhYiwgc28gd3JpdGU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