Deadline: Tuesday, 14 September 2022 at the start of class
This exam is the same format as the notebooks we have been completing in class. In order to complete it correctly, please keep in mind that:
In each section there is exactly one plot that you need to create. It will often require using data verbs to create the plot, but it is up to you how to do that.
There are nine questions on this exam. Each is worth 10 points. An additional 10 points is assigned based on your code formatting across the entire exam. This take-home exam will count for half of your Exam 01 grade.
You must Knit the file to an HTML format, print the file, and then bring the exam to class on Wednesday. Some questions require you to build a plot with specific colors; you do not need to print the HTML in color. I will be able to tell if you did the correct thing based on the code.
You may use any static resources, such as course notes and external websites, but you may not discuss the exam with classmates anyone else.
I am happy to answer clarifying questions about the exam or to help you with unexpected R errors. However, I will not answer content-based questions after the exam is posted. Note that I may not be able to answer questions sent after 8pm on Tuesday night.
The exam should take no more than 2 hours, but you may use as much time as you need.
Personal computer issues is not an excuse for not completing the exam on time. As a back up, you can use the computers in the Jepson computer lab or in the library.
Good luck!
In the code block below, create a scatter plot with longitude on the x-axis, latitude on the y-axis, and one point for each shark attack in the data.
%>%
sharks ggplot(aes(lon, lat)) +
geom_point()
In the code block below, create a scatter plot with longitude on the x-axis, latitude on the y-axis, and one point for each shark attack in the data. Color the points based on the outcome of the attack (fatal, injured, or uninjured). Use the color-blind friendly color scale.
%>%
sharks ggplot(aes(lon, lat)) +
geom_point(aes(color = outcome)) +
scale_color_viridis_d()
In the code block below, create a bar plot with shark type on the y-axis and the number of attacks for a given shark type on the x-axis. Arrange the sharks from the smallest to the largest number of attacks (i.e., the shark with the lowest number of attacks should be in in the upper-left hand corner of the plot).
%>%
sharks group_by(shark_type) %>%
summarize(n = n()) %>%
arrange(desc(n)) %>%
ggplot(aes(n, fct_inorder(shark_type))) +
geom_col()
In the code block below, create a scatter plot with longitude on the x-axis, latitude on the y-axis, and one point for each shark attack in the data. Color the points in the light grey color “grey85”, while highlighting the attacks made by the type of shark with the most attacks (at determined in the previous question) in “red”.
# How I expected you to do this
<- sharks %>%
white_shark filter(shark_type %in% c("white shark"))
%>%
sharks ggplot(aes(lon, lat)) +
geom_point(color = "grey85") +
geom_point(color = "red", data = white_shark)
# Another approach with mutate()
%>%
sharks mutate(my_color = if_else(shark_type == "white shark", "red", "grey85")) %>%
arrange(my_color) %>%
ggplot(aes(lon, lat)) +
geom_point(aes(color = my_color)) +
scale_color_identity()
In the code block below, draw a line plot (geom_line
)
with year on the x-axis and the number of shark attacks in a given year
on the y-axis.
%>%
sharks group_by(year) %>%
summarise(n = n()) %>%
ggplot(aes(year, n)) +
geom_line()
In the code block below, draw a line plot (geom_line
)
with year on the x-axis and the number of shark attacks in a given year
on the y-axis just as you did in the previous question. This time, add a
red dot showing the year with the greatest number of attacks. The dot
should be located at the year with the maximum number of attacks
(x-axis) and the maximum number of attacks (y-axis).
<- sharks %>%
most_attacks group_by(year) %>%
summarise(n = n()) %>%
arrange(desc(n)) %>%
slice(1)
%>%
sharks group_by(year) %>%
summarise(n = n()) %>%
ggplot(aes(year, n)) +
geom_line() +
geom_point(data = most_attacks, color = "red")
In the code block below, create a scatter plot with the victim activity on the y-axis and the percentage of attacks that were fatal for victims engaged in that activity on the x-axis. Order the activities from the least-likely to be fatal to the most likely to be fatal.
%>%
sharks group_by(victim_activity) %>%
summarise(fatal_mean = mean(outcome == "fatal")) %>%
arrange(desc(fatal_mean)) %>%
ggplot(aes(fatal_mean, fct_inorder(victim_activity))) +
geom_point()