Assignment 2

Due: Monday, May 26 at 11 AM ET to be submitted via Crowdmark

Note: Your submitted solution must be unique and written in your own words; if you collaborate with other students, external resources, and/or use AI tools, this must be clearly acknowledged in your submission.

Part 1

This assignment covers material from Chapters 9-14 of R4DS / Chapters 11-17 of P4DS. Please complete the ALL of following exercises from ONE of those textbooks.

  1. (9.2.1 Q2 / 12.2 Q2) Why did the following code not result in a plot with blue points?
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = "blue"))
  1. (9.2.1 Q3 / 12.2 Q3) What does the stroke aesthetic do? What shapes does it work with? (Hint: use ?geom_point)

  2. (9.3.1 Q1 / 12.3.1 Q1) What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?

  3. (9.4.1 Q4 / 12.4.1 Q4) Take the first faceted plot in this section:

    ggplot(mpg) + 
      geom_point(aes(x = displ, y = hwy)) + 
      facet_wrap(~ cyl, nrow = 2)

    What are the advantages to using faceting instead of the color aesthetic? What are the disadvantages? How might the balance change if you had a larger dataset?

  4. (9.6.1 Q1 / 12.6.1 Q1) What is the problem with the following plot? How could you improve it?

    ggplot(mpg, aes(x = cty, y = hwy)) + 
      geom_point()
  5. (10.3.3 Q2 / 13.3.3 Q2) Explore the distribution of price. Do you discover anything unusual or surprising? (Hint: Carefully think about the binwidth and make sure you try a wide range of values.)

  6. (10.4.1 Q1 / 13.4.1 Q1) What happens to missing values in a histogram? What happens to missing values in a bar chart? Why is there a difference in how missing values are handled in histograms and bar charts?

  7. (10.5.1.1 Q5 / 13.5.1.1 Q3) Create a visualization of diamond prices vs. a categorical variable from the diamondsdataset using geom_violin(), then a faceted geom_histogram(), then a colored geom_freqpoly(), and then a colored geom_density(). Compare and contrast the four plots. What are the pros and cons of each method of visualizing the distribution of a numerical variable based on the levels of a categorical variable?

  8. (10.5.2.1 Q3 / 13.5.3 Q3) Use geom_tile() together with dplyr to explore how average flight departure delays vary by destination and month of year. What makes the plot difficult to read? How could you improve it?

  9. (11.3.1 Q3 / 14.3.1 Q3) How do labels with geom_text() interact with faceting? How can you add a label to a single facet? How can you put a different label in each facet? (Hint: Think about the dataset that is being passed to geom_text().)

  10. (13.5.4 Q7 / 16.3.3 Q6) Look at each destination. Can you find flights that are suspiciously fast (i.e. flights that represent a potential data entry error)? Compute the air time of a flight relative to the shortest flight to that destination. Which flights were most delayed in the air?

  11. (13.6.7 Q1 / 16.4.3 Q1) Brainstorm at least 5 different ways to assess the typical delay characteristics of a group of flights. When is mean() useful? When is median() useful? When might you want to use something else? Should you use arrival delay or departure delay? Why might you want to use data from planes?

R4DS P4DS
9.2.1 Q2 12.2 Q2
9.2.1 Q3 12.2 Q3
9.3.1 Q1 12.3.1 Q1
9.4.1 Q4 12.4.1 Q4
9.6.1 Q1 12.6.1 Q1
10.3.3 Q2 13.3.3 Q2
10.4.1 Q1 13.4.1 Q1
10.5.1.1 Q5 13.5.1.1 Q3
10.5.2.1 Q3 13.5.3 Q3
11.3.1 Q3 14.3.1 Q3
13.5.4 Q7 16.3.3 Q6
13.6.7 Q1 16.4.3 Q1

Part 2

Using a sports dataset from a library/package of your choice, perform the following steps of the data science workflow:

  1. Visualize
    • Create a visualization with customized title, subtitle, caption, x, y, and color labels.

      1. Modify the theme element of this visualization to make the axis labels blue and bolded.
      2. Annotate a specific item in your visualization with a brief description, and use another aesthetic to differentiate that item from the others.
  2. Model
    • Apply a statistical technique learned from a previous course (such as a linear model or statistical test) to variables from dataset and comment briefly on your findings.