Quiz-1

Short answer solutions

Question 9: Suppose a researcher is writes the following code to create a scatter plot between penguin body mass and penguin flipper length. They also want to color the points blue.

The following code results in this plot:

In as much detail as possible…

Explain why the dots are not blue How you would fix the code above to make the dots blue

Solution:

Need to be clear and articulate that the dots are not blue because color = “blue” is in the aes. The aes function maps variables from our dataset to our ggplot. In this example, R has generated a variable called colour with a single level (blue), and mapped this variable to each plotted observations in the scatterplot.

You would instead put color=“blue” inside the function geom_point().

Credit was given for fill = “blue” in gemo_point(). However, this code would not run, and geom_point is the one exception to the rule that color = outlines the geometric shape, and fill = fills the geometric shape in with color.

Question 10: A researcher was trying to create side-by-side boxplots of the penguin’s body mass by their sex. They wrote the following code:

There are 3 distinct issues in the above code that prevent the boxplot from being created using the penguins dataset we are familiar in class with. Below, number and identify the three problems with the code:

Example:

Issue 1

Issue 2

Issue 3

Solution:

Need to have a pipe operator (|> or %>% is fine) instead of a + sign

body_mass needs to be body_mass_g

missing comma between the x = argument and y = argument