For this assignment you should record your answers in an R Markdown file and submit the compiled output as a pdf.
Homework #3 is due Friday 10/09 by 11:59pm
The data frame diamonds is contained within the
ggplot2 package. These data record the attributes of
several thousand diamonds sold by a wholesale online retailer. For this
question, your goal is to recreate the graph shown below as closely as
possible, with one exception: pick a better color scheme. A few
hints:
alpha = 0.3 is used to give each point 30%
opacity.library(ggplot2) # Make sure you have this package installed
data("diamonds") # loads the built-in "diamonds" data set
\(~\)
Consider the following ggplot code on the
mpg dataset.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
Part A: Why are the data-points in the scatter plot not colored blue? Briefly explain.
Part B: Modify the code so that the points are properly shown as blue.
Part C: Create two different types of visualizations
that investigate relationships among engine displacement
(displ), highway fuel efficiency (hwy), number
of cylinders (cyl), and manufacturer. Across
your two visualizations, you should make meaningful use of all four
variables. Briefly describe the patterns you observe in each
visualization.
You may not use a scatterplot for either visualization. Your two visualizations should reveal different aspects of the data rather than simply presenting the same information using different types of plots.
Using the diamonds data, create two different types of
visualizations that investigate how the distribution of
price differs among different diamond cuts. Briefly
describe what each visualization reveals, then compare and contrast the
advantages and disadvantages of the two approaches for investigating
this question. You may not use histograms for this
question.
Suppose we are interested in whether the length of a flight is
related to the amount of delay experienced when the flight arrives at
its destination. The following visualization uses the
flights_sample data from lab to investigate the
relationship between flight distance and arrival delay.
ggplot(flights_sample, aes(x = distance, y = arr_delay)) +
geom_point()
Part A: Examine the visualization above. Identify at least one feature of the data that makes this graph difficult to use for investigating the relationship between distance and arr_delay. Explain what information is difficult to see as a result.
Part B: Create an improved visualization that makes the relationship between flight distance and arrival delay easier to investigate. Briefly explain the graphical choices you made.
Part C: The relationship between distance and arrival delay may not be the same for flights leaving each of the three New York airports. Extend your investigation to determine whether the relationship differs among the airports in origin. You may modify your visualization from Part B or create a new visualization if you think a different approach is more appropriate.
\(~\)
# code for creating the flights_sample dataset
library(nycflights13)
set.seed(230)
flights_sample = flights |>
filter(
carrier %in% c("AA", "B6", "DL", "UA", "WN"),
!is.na(arr_delay)
) |>
slice_sample(n = 5000) |>
select(
year, month, day, dep_time, sched_dep_time,
dep_delay, arr_time, sched_arr_time, arr_delay,
carrier, flight, origin, dest, air_time, distance
)
flights_sample = flights_sample |>
mutate(
airline = recode(
carrier,
AA = "American",
B6 = "JetBlue",
DL = "Delta",
UA = "United",
WN = "Southwest"
)
)
Two additional questions were added on Monday. My wife and I use a simple rule for household chores: the task has to get done eventually, so putting it off is really just punishing Future You. Knock it out, enjoy the peace of mind, and let Future You have two less questions thing to worry about.