Data Visualization - Extra Practice

Practice Questions (True or False)

In the following practice problems, assume that you have the following data frame df

   name age score subject
1 Alice  25    90    Math
2   Bob  30    85    Math
3 Carol  22    88 Science
4 David  35    95 Science
5   Eva  28    76    Math

Exercise 1

True or False. This code will show a boxplot of scores for each subject.

ggplot(df, aes(x = subject, y = score)) +
  geom_boxplot()
Show answer

Answer: True.

Exercise 2

True or False. This code creates a histogram—with a binwidth of 5—of the score values across all students.

ggplot(df, aes(x = score)) +
  geom_histogram(binwidth = 5)
Show answer

Answer: True.

Exercise 3

True or False. This code creates a scatter plot of age -vs- score.

ggplot(df, aes(x = age, y = score)) +
  geom_scatter()
Show answer

Answer: False. Use geom_point() instead of geom_scatter()

Exercise 4

True or False. The following command creates a bar plot showing the score per subject for each student.

ggplot(df, aes(x = subject, y = score)) +
  geom_col()
Show answer

Answer: False. geom_col() shows one bar per row by default unless aggregated first

Exercise 5

True or False. This code creates a bar plot with the score for each student, filled based on their subject.

ggplot(df, aes(x = name, y = score, fill = subject)) +
  geom_col()
Show answer

Answer: True.

Exercise 6

True or False. This will draw a line chart showing score by student.

ggplot(df, aes(x = name, y = score)) +
  geom_line()
Show answer

Answer: False. For this command to work, you need to add group = 1 inside aes()

Practice Questions (Datasaurus)

The following practice problems are based on the datasaurus_dozen data set from the R package "datasauRus".

  1. Launch RStudio, and run the following command in the console to install the R package "datasauRus":
# run command in R's console
install.packages("datasauRus")
  1. Use library() to load packages "tidyverse" and "datasauRus".

  2. Use base R head() or tidyverse slice_head() to inspect the first few rows of datasaurus_dozen.

Exercise 7

Write a dplyr command that gives you the unique values (i.e. categories) of the datasaurus_dozen column dataset.`.

Show answer
# equivalent commands
distinct(datasaurus_dozen, dataset)
count(datasaurus_dozen, dataset)

Exercise 8

Write a single dplyr pipeline to subset the datasaurus_dozen rows that belong to the dataset value "bullseye", and obtain the mean and standard deviation of columns x and y, as well as the correlation between x and y.

Show answer
datasaurus_dozen |> 
  filter(dataset == "bullseye") |> 
  summarize(mean(x), mean(y), sd(x), sd(y), cor(x, y))

Exercise 9

Repeat the above pipeline for the rows with a dataset value of "x_shape". How do these summaries compare to those obtained in exercise 2?

Show answer
datasaurus_dozen |> 
  filter(dataset == "x_shape") |> 
  summarize(mean(x), mean(y), sd(x), sd(y), cor(x, y))

Exercise 10

Write ggplot2 code to graph a scatterplot of x and y for the "bullseye" dataset in datasaurus_dozen.

Show answer
datasaurus_dozen |> 
  filter(dataset == "bullseye") |> 
  ggplot(aes(x = x, y = y)) +
  geom_point()

Exercise 11

Graph another scatterplot of x and y for the "x_shape" dataset in datasaurus_dozen. How do these graphics compare to each other?

Show answer
datasaurus_dozen |> 
  filter(dataset == "x_shape") |> 
  ggplot(aes(x = x, y = y)) +
  geom_point()

Exercise 12

Now write a single dplyr pipeline to obtain the mean and standard deviation of columns x and y, as well as the correlation between x and y, for every dataset category. What do you notice among the summaries for each dataset type?

Show answer
datasaurus_dozen |> 
  group_by(dataset) |> 
  summarize(
    mean_x    = mean(x),
    mean_y    = mean(y),
    std_dev_x = sd(x),
    std_dev_y = sd(y),
    corr_xy   = cor(x, y)
  )

Exercise 13

Finally, write ggplot2 code to obtain, in a single graphic, scatter plots for all the dataset types.

- To achieve this you need to use the function `facet_wrap()`, e.g., `facet_wrap(vars(dataset))`. 

- Color code by `dataset`, and apply a theme of your choosing.

- Add a meaningful title and subtitle to your plot.

- Once you are happy with your graphic, handwrite your code in the space below.
Show answer
ggplot(datasaurus_dozen, aes(x = x, y = y, color = dataset)) +
  geom_point(show.legend = FALSE) +
  facet_wrap(vars(dataset)) +
  theme_minimal() +
  labs(
    title = "The Datasaurus Dozen",
    subtitle = "Same stats, completely different shapes!"
  )