7.4.4.Weekly challenge 4 - sj50179/Google-Data-Analytics-Professional-Certificate GitHub Wiki

Weekly challenge 4

LATEST SUBMISSION GRADE 100%

Question 1

Which of the following are benefits of using ggplot2? Select all that apply.

  • Easily add layers to your plot
  • Combine data manipulation and visualization
  • Automatically clean data before creating a plot
  • Customize the look and feel of your plot

Correct. The benefits of using ggplot2 include easily adding layers to your plot, customizing the look and feel of your plot, combining data manipulation and visualization.

Question 2

A data analyst creates a bar chart with the diamonds dataset. They begin with the following line of code:

ggplot(data = diamonds)

What symbol should the analyst put at the end of the line of code to add a layer to the plot?

  • The equal sign (=)
  • The plus sign (+)
  • The pipe operator (%>%)
  • The ampersand symbol (&)

Correct. The analyst should put the plus sign (+) at the end of the line of code to add a layer to the plot. The first line of code is ggplot(data = diamonds) +*.*

Question 3

A data analyst creates a plot using the following code chunk:

ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

Which of the following represents a function in the code chunk? Select all that apply.

  • The aes function
  • the data function
  • The geom_point function
  • The ggplot function

Correct. The functions in the code chunk are the ggplot() function, the geom_point() function, and the aes() function. The ggplot() function specifies the data frame to use for the plot. The geom_point() function specifies the geometric object that represents the data. The aes() function specifies the aesthetic attributes of the plot.

Question 4

Fill in the blank: In ggplot2, the term mapping refers to the connection between variables and _____ .

  • facets
  • data frames
  • geoms
  • aesthetics

Correct. Mapping means matching up a specific variable in your data set with a specific aesthetic. You use the aes() function to define the mapping between your data and your plot.

Question 5

A data analyst creates a scatterplot with a lot of data points. The analyst wants to make some points on the plot more transparent than others. What aesthetic should the analyst use?

  • Fill
  • Shape
  • Alpha
  • Color

Correct. The analyst should use the alpha aesthetic. The alpha aesthetic makes some points on a plot more transparent than others.

Question 6

You are working with the penguins dataset. You create a scatterplot with the following code:

ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

You want to highlight each penguin species in your plot. Add a code chunk to the second line of code to map the aesthetic color to the variable species.

NOTE: the three dots (...) indicate where to add the code chunk.

ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species))

Which penguin species does your visualization display?

  • Adelie, Chinstrap, Gentoo
  • Chinstrap, Emperor, Gentoo
  • Adelie, Chinstrap, Macaroni
  • Adelie, Emperor, Gentoo

Correct. You add the code chunk color = species to the second line of code to map the aesthetic color to the variable species. The correct code is ggplot(data = penguins) + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species))*. Inside the parentheses of the aes() function, after the comma that follows y = body_mass_g, write the aesthetic (color), then an equals sign, then the variable (species). The data points for each penguin species now appear in different colors. Your visualization displays the Adelie, Chinstrap, and Gentoo penguin species.*

Question 7

A data analyst creates a plot with the following code chunk:

ggplot(data = penguins) +
    geom_jitter(mapping = aes(x = flipper_length_mm, y = body_mass_g))

What does the geom_jitter() function do to the points in the plot?

  • Adds random colors to each point in the plot
  • Decrease the size of each point in the plot
  • Adds a small amount of random shapes at each point in the plot
  • Adds a small amount of random noise to each point in the plot

Correct. The geom_jitter() function creates a scatterplot and then adds a small amount of random noise to each point in the plot to make the points easier to find.

Question 8

You are working with the diamonds dataset. You create a bar chart with the following code:

ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = color, fill = cut)) +

You want to use the facet_wrap() function to display subsets of your data. Add the code chunk that lets you facet your plot based on the variable cut.

ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = color, fill = cut)) +
    facet_wrap(~cut)

How many subplots does your visualization show?

  • 4
  • 6
  • 5
  • 3

Correct. You add the code chunk facet_wrap(~cut) to facet your plot based on the variable cut. The correct code is ggplot(data = diamonds) + geom_bar(mapping = aes(x = color, fill = cut)) + facet_wrap(~cut)*. Inside the parentheses of the* facet_wrap() function, write a tilde symbol (~) followed by the name of the variable you want to facet. The facet_wrap() function lets you display subsets of your data. Your visualization shows 5 subplots.

Question 9

A data analyst uses the annotate() function to create a text label for a plot. Which attributes of the text can the analyst change by adding code to the argument of the annotate() function? Select all that apply.

  • Change the font style of the text
  • Change the size of the text
  • Change the text into a title for the plot
  • Change the color of the text

Correct. By adding code to the argument of the annotate() function, the analyst can change the font style, color, and size of the text.

Question 10

You are working with the penguins dataset. You create a scatterplot with the following lines of code:

ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g)) +

What code chunk do you add to the third line to save your plot as a png file with “penguins” as the file name?

  • ggsave(“penguins.png”)
  • ggsave(“png.penguins”)
  • ggsave(penguins.png)
  • ggsave(“penguins”)

Correct. You add the code chunk ggsave("penguins.png") to save your plot as a png file with “penguins” as the file name. Inside the parentheses of the ggsave() function, type a quotation mark followed by the file name (penguins), then a period, then the type of file (png), then a closing quotation mark.