In ggplot2 you can use the _ function to specify the data frame to use for your plot

Course 7 – Data Analysis with R Programming Quiz Answers

Week 4: More About Visualization, Aesthetics, and Annotations

GOOGLE DATA ANALYTICS PROFESSIONAL CERTIFICATE

Coursera RStudio Answers Study Guide

More About Visualization, Aesthetics, and Annotations INTRODUCTION

In this Coursera Google Data Analytics Professional Certification course, you’ll continue to explore visualization techniques in R and learn how to generate and troubleshoot visualizations. You’ll gain insight into the features of RStudio that help customize your charts for greater aesthetic appeal, with options for annotating and saving them. As a result, you will be able to create detailed visualizations that are easily shareable with others or incorporated into other presentations.

Using R can be intimidating at first glance. Still, with practice and application of the material learned in this part of the course, you’ll soon become confident in creating attractive visuals, and kind of knowledge is invaluable when it comes to data analytics!

Learning Objectives

  • Identify the aesthetics features available in R with reference to size, shape, color, and plots
  • Explain some common problems associated with visualizations in R
  • Use of ggplot() to generate basic visualizations
  • Describe the options for generating visualizations in R
  • Demonstrate an understanding of RStudio functionality for saving visualizations
  • Create a plot in ggplot2
  • Explain the purpose and basic logic of the ggplot2 package

Test your knowledge on data visualization in r

1. In ggplot2, you can use the _____ function to specify the data frame to use for your plot.

  • geom_point()
  • aes()
  • labs()
  • ggplot() (Correct)

In ggplot2 you can use the _ function to specify the data frame to use for your plot.

Correct: In ggplot2, you can use the ggplot() function to specify the data frame to use for your plot.

2. In ggplot2, you use the plus sign (+) to add a layer to your plot.

  • True (Correct)
  • False

Correct: In ggplot2, you use the plus sign (+) to add a layer to your plot.

3. In ggplot2, what function do you use to map variables in your data to visual features of your plot?

  • The ggplot() function
  • The aes() function (Correct)
  • The geom_bar() function
  • The geom_point() function

Correct: In ggplot2, you use the aes() function to map variables in your data to visual features of your plot. These features are known as aesthetics.

4. What type of plot will the following code create?

ggplot(data = penguins) +

     geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

  • Boxplot
  • Bar chart
  • Scatterplot (Correct)
  • Line diagram

Correct: The code will create a scatterplot. The function geom_point() uses points to create a scatterplot.

Test your knowledge on aesthetics in analysis

1. Which of the following aesthetics attributes can you map to the data in a scatterplot? Select all that apply.

  • Text
  • Shape (Correct)
  • Size (Correct)
  • Color (Correct)

Correct: You can map the color, shape, and size aesthetics to the data in a scatterplot.

2. Which of the following functions let you display smaller groups, or subsets, of your data?

  • ggplot()
  • facet_wrap() (Correct)
  • geom_point()
  • geom_bar()

Correct: The facet_wrap() function lets you display smaller groups, or subsets, of your data.

3. What is the role of the x argument in the following code?

ggplot(data = diamonds) +

     geom_bar(mapping = aes(x = cut))

  • A function
  • An aesthetic (Correct)
  • A variable
  • A dataset

Correct: X is an aesthetic that refers to the x-axis of the plot. The x aesthetic maps the variable cut from the diamonds dataset to the x-axis of the plot.

4. A data analyst creates a scatterplot with a lot of data points. It is difficult for the analyst to distinguish the individual points on the plot because they overlap. What function could the analyst use to make the points easier to find?

  • geom_jitter() (Correct)
  • geom_bar()
  • geom_line()
  • geom_point()

Correct: The analyst could use the geom_jitter() function to make the points easier to find. The geom_jitter() function adds a small amount of random noise to each point in the plot, which helps deal with the overlapping of points.

5. You can use this color aesthetic to add color to the outline of each bar in a bar chart.

  • True (Correct)
  • False

test your knowledge on annotating and saving visualization

1. Which of the following are benefits of adding labels and annotations to your plot? Select all that apply.

  • Indicating the main purpose of your plot (Correct)
  • Choosing a geom for your plot
  • Highlighting important data in your plot (Correct)
  • Helping stakeholders quickly understand your plot (Correct)

Correct: The benefits of adding annotations to your plot include indicating the main purpose of your plot, highlighting important data in your plot, and helping stakeholders quickly understand your plot.

2. A data analyst is creating a plot for a presentation to stakeholders. The analyst wants to add a caption to the plot to help communicate important information. What function could the analyst use?

  • The geom_bar() function
  • The facet_wrap() function
  • The geom_point() function
  • The labs() function (Correct)

Correct: The analyst could use the labs() function to add a caption to the plot.

3. What function can you use to put a text label inside the grid of your plot to call out specific data points?

  • The aes() function
  • The annotate() function (Correct)
  • The facet_wrap() function
  • The labs() function

Correct: You can use the annotate() function to put a text label inside the grid of your plot to call out specific data points.

4. 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 use the labs() function to add the title “Penguins” to your plot. Add the code chunk that lets you add the title “Penguins” to your plot.

labs(title = “Penguins”)

labs(title = "Penguinds")

Where does your visualization display the title?

  • The upper right
  • The upper left (Correct)
  • The lower right
  • The lower left

Correct: You add the code chunk labs(title = “Penguins”) to add the title “Penguins” to your plot. Inside the parentheses of the labs() function, write the word title, then an equals sign, then the specific text of the title in quotation marks. The labs() function lets you add labels to your plot.

Your visualization displays the title in the upper left.

GOOGLE DATA ANALYTICS COURSERA ANSWERS AND STUDY GUIDE

Liking our content? Then don’t forget to add us to your bookmarks so you can find us easily!

Weekly Breakdown | Google Study Guides | Back to Top

Data Analysis with R Programming Weekly Challenge 4

1. Which of the following tasks can you complete with ggplot2 features? Select all that apply.

  • Customize the visual features of a plot (Correct)
  • Automatically clean data before creating a plot
  • Add labels and annotations to a plot (Correct)
  • Create many different types of plots (Correct)

Correct: ggplot2 includes features that let you create many different types of plots, customize the visual features of a plot, and add labels and annotations to a plot.

2. In ggplot2, what symbol do you use to add layers to your plot?

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

Correct: In ggplot2, you use the plus sign (+) to add layers to your plot.

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 (Correct)
  • The geom_point function (Correct)
  • the data function
  • The ggplot function (Correct)

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.

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

  • facets
  • geoms
  • aesthetics (Correct)
  • data frames

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.

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?

  • Alpha (Correct)
  • Shape
  • Fill
  • Color

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

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 the different penguin species on your plot. Add a code chunk to the second line of code to map the aesthetic shape to the variable species.

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

geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, shape = species))

 Adelie, Chinstrap, Gentoo

Which penguin species does your visualization display?

  • Adelie, Gentoo, Macaroni
  • Adelie, Chinstrap, Emperor
  • Adelie, Chinstrap, Gentoo (Correct)
  • Emperor, Chinstrap, Gentoo

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

Your visualization displays the Adelie, Chinstrap, and Gentoo penguin species.

7. Fill in the blank: The _____  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.

  • geom_bar() function
  • geom_point() function
  • geom_jitter() function (Correct)
  • geom_smooth() function

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.

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.

facet_wrap(~cut)

facet_wrap(~cut)

How many subplots does your visualization show?

  • 5 (Correct)
  • 3
  • 6
  • 4

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.

9. Fill in the blank: You can use the _____ function to put a text label on your plot to call out specific data points.

  • facet_grid()
  • annotate() (Correct)
  • geom_smooth()
  • ggplot()

Correct: You can use the annotate() function to put a text label on your plot to call out specific data points.

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(“penguins”)
  • ggsave(“penguins.png”) (Correct)
  • ggsave(“png.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.

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

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

12. A data analyst uses the aes() function to define the connection between their data and the plots in their visualization. What argument is used to refer to matching up a specific variable in your data set with a specific aesthetic?

  • Faceting
  • Mapping (Correct)
  • Jittering
  • Annotating

13. A data analyst is working with the penguins data. The analyst creates a scatterplot with the following code:

ggplot (data = penguins) +

     geom_point (mapping = aes (x = flipper_length_mm, y = body_mass_g, alpha = species))

What does the alpha aesthetic do to the appearance of the points on the plot?

  • Makes some point of the plot more transparent (Correct)
  • Makes the points on the plot more colorful
  • Makes the points on the plot smaller
  • Makes the points on the plot larger

14. 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 color.

facet_wrap(~color)

How many subplots does your visualization show?

  • 6
  • 8
  • 9
  • 7 (Correct)

Correct: You add the code chunk facet_wrap(~color) to facet your plot based on the variable color. The correct code is ggplot(data = diamonds) + geom_bar(mapping = aes(x = color, fill = cut)) + facet_wrap(~color). 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 7 subplots.

15. 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 size of the text (Correct)
  • Change the font style of the text (Correct)
  • Change the color of the text (Correct)
  • Change the text into a title for the plot

16. 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 the different years of data collection on your plot. Add a code chunk to the second line of code to map the aesthetic size to the variable year.

NOTE: the three dots (…) indicate where to add the code chunk. You may need to scroll in order to find the dots.

geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, …))

What years does your visualization display?

  • 2005-2009
  • 2007-2009 (CORRECT)
  • 2007-2011
  • 2006-2010

Correct: You add the code chunk size = year to map the aesthetic size to the variable year. The correct code is ggplot(data = penguins) + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, size = year)). Inside the parentheses of the aes() function, after the comma that follows y = body_mass_g, write the aesthetic (size), then an equals sign, then the variable (year). The data points for the different years now appear in different sizes.

Your visualization displays the years 2007-2009.

17. Fill in the blank: The _____ 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.

  • geom_jitter() function (CORRECT)
  • geom_smooth() function
  • geom_point() function
  • geom_bar() function

18. What function can be used to facet a plot on two variables?

  • geom_wrap()
  • facet_grid() (CORRECT)
  • facet_layout()
  • facet_wrap()

19. What argument of the labs() function can a data analyst use to add text outside of the grid area of a plot?

  • text
  • annotate
  • title (CORRECT)
  • note

20. In R studio, what default options does the Export functionality of the Plots tab give for exporting plots?

  • HTML
  • Slideshow
  • Image (CORRECT)
  • PDF (CORRECT)

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

ggplot(data = penguins) +

   geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

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

NOTE: the three dots (…) indicate where to add the code chunk. You may need to scroll in order to find the dots.

geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, …))

Which approximate range of bill depths does your visualization display?

  • 20 – 31
  • 14 – 20 (CORRECT)
  • 31 – 40
  • 2 – 9

22.  What function can be used to facet a plot on two variables?

  • geom_wrap()
  • facet_grid() (CORRECT)
  • facet_layout()
  • facet_wrap()

23. Which of the following is a functionality of ggplot2?

  • Combine data manipulation and visualizations using pipes. (CORRECT)
  • Filter and sort data in complex ways.
  • Define complex visualization using a single function.
  • Create plots using artificial intelligence.

24. A data analyst creates a scatterplot. The analyst wants to put a text label on the plot to call out specific data points. What function does the analyst use?

  • The annotate() function (CORRECT)
  • The geom_smooth() function
  • The facet_grid() function
  • The ggplot() function

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

ggplot(data = penguins) +

   geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

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

NOTE: the three dots (…) indicate where to add the code chunk. You may need to scroll in order to find the dots.

geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, …))

Which species tends to have the longest flipper length and highest body mass?

  • Chinstrap
  • Adelie
  • Gentoo (CORRECT)
  • Macaroni

26. What is the purpose of the facet_wrap() function?

  • Create text inside a plot area
  • Create subplots of a single variable into separate categories (CORRECT)
  • Modify the visual characteristic of a data point
  • Modify ggplot visuals to be three-dimensional

27. Which statement about the ggsave() function is correct?

  • ggsave() is the only way to export a plot.
  • ggsave() exports the last plot displayed by default. (CORRECT)
  • ggsave() is run from the Plots Tab in RStudio.
  • ggsave() is unable to save .png files.

28. A data analyst creates a scatterplot where the points are very crowded, which makes it hard to notice when points are stacked. What change can they make to their scatter plot to make it easier to notice the stacked data points?

  • Change geom_point() to geom_jitter()(CORRECT)
  • Change the shape of the points
  • Change ggplot() to ggplot2()
  • Change the color of the points

29. A data analyst wants to add a large piece of text above the grid area that clearly defines the purpose of a plot. Which ggplot function can they use to achieve this?

  • annotate()
  • labs() (CORRECT)
  • subtitle()
  • title()

30. 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 clarity.

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

how many subplots does your visualization show?

  • 8
  • 9
  • 6
  • 7 (CORRECT)

More About Visualization, Aesthetics, and Annotations CONCLUSION

In this part of the course, you learned how to use R to generate detailed visualizations. You also explored the features of RStudio that will help you with the aesthetics of your visualizations.

These skills will be useful for creating annotated and saved visualizations. If you want to learn more about using R for data visualization, join the Coursera learning experience today.