7.4.3.Annotate and save visualizations - quanganh2001/Google-Data-Analytics-Professional-Certificate-Coursera GitHub Wiki

Adding annotations in R

Annotations are a useful way to add notes to your plot. They help you explain the plot’s purpose, highlight important data points, or comment on any data trends or findings the plot illustrates. You have already learned how to add notes as labels, titles, subtitles, and captions. You can also draw arrows or add shapes to your plot to create more emphasis. Usually you add these kinds of annotations in your presentation application after you have saved the visualizations. But, you can now add lines, arrows, and shapes to your plots using ggplot2.

Resources

Check out these resources to learn more:

  • Create an annotation layer: This guide explains how to add an annotation layer with ggplot2. It includes sample code and data visualizations with annotations created in ggplot2.
  • How to annotate a plot in ggplot2: This resource includes explanations about how to add different kinds of annotations to your ggplot2 plots, and is a great reference if you need to quickly look up a specific kind of annotation.
  • Annotations: Chapter eight of the online ggplot2 textbook is focused entirely on annotations. It provides in-depth explanations of the different types of annotations, how they are used, and detailed examples.
  • How to annotate a plot: This R-Bloggers article includes explanations about how to annotate plots in ggplot2. It starts with basic concepts and covers more complicated information the further on you read.
  • Text Annotations: This resource focuses specifically on adding text annotations and labels to ggplot2 visualizations.

Saving images without ggsave()

In most cases, ggsave() is the simplest way to save your plot. But there are situations when it might be best to save your plot by writing it directly to a graphics device. This reading will cover some of the different ways you can save images and plots without ggsave(), and includes additional resources to check out if you want to learn more.

A graphics device allows a plot to appear on your computer. Examples include:

  • A window on your computer (screen device)
  • A PDF, PNG, or JPEG file (file device)
  • An SVG, or scalable vector graphics file (file device)

When you make a plot in R, it has to be “sent” to a specific graphics device. To save images without using ggsave(), you can open an R graphics device like png() or pdf(); these will allow you to save your plot as a .png or .pdf file. You can also choose to print the plot and then close the device using dev.off().

image

To learn more about the different processes for saving images, check out these resources:

  • Saving images without ggsave(): This resource is pulled directly from the ggplot2 documentation at tidyverse.org. It explores the tools you can use to save images in R, and includes several examples to follow along with and learn how to save images in your own R workspace.
  • How to save a ggplot: This resource covers multiple different methods for saving ggplots. It also includes copyable code with explanations about how each function is being used so that you can better understand each step in the process.
  • Saving a plot in R: This guide covers multiple file formats that you can use to save your plots in R. Each section includes an example with an actual plot that you can copy and use for practice in your own R workspace.

Hands-On Activity: Annotating and saving visualizations

Activity overview

UWFf-U9hTzKhX_lPYX8yBw_8c2e9cd211e3479a89816c7b1816ab07_image4

So far, you have used ggplot2 to create different kinds of visualizations. In this activity, you’ll follow through a scenario and add annotations to a data visualization with ggplot2. You will also learn how to save images from ggplot2 visualizations.

By the end of this activity, you will be able to enhance a visualization with annotations and save it as an image so that you can add it directly to a presentation. This will enable you to demonstrate your findings more clearly and better explain your insights in your career as a data analyst.

Working in RStudio Cloud

UWFf-U9hTzKhX_lPYX8yBw_8c2e9cd211e3479a89816c7b1816ab07_image4

To start, log into your RStudio Cloud account. Open the project you will work on in the activity with this link, which opens in a new tab. If you haven't gone through this process already, at the top right portion of the screen you will see a "red stamp" indicating this project as a Temporary Copy. Click on the adjacent button, Save a Permanent Copy, and the project will be saved in your main dashboard for use with future lessons. Once that is completed, navigate to the file explorer in the bottom right and click the following: Course 7 -> Week 4 -> Lesson4_Annotations.Rmd.

The .csv file that you will need, hotel_bookings.csv, is also located in this folder.

If you’re having trouble finding the correct activity, check out this step-by-step guide on how to navigate in RStudio Cloud. Make sure to select the correct R markdown (Rmd) file. The other Rmd files will be used in different activities.

If you are using RStudio Desktop, you can download the Rmd file and the data for this activity directly here:

Lesson4_Annotations

hotel_bookings

You can also find the Rmd file with the solutions for this activity here:

Lesson4_Annotations_Solutions

Carefully read the instructions in the comments of the Rmd file and complete each step. Some steps may be as simple as running pre-written code, while others may require you to write your own functions. After you finish the steps in the Rmd file, return here to confirm that your work is complete.

Confirmation

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

After running the following code below to save the plot as a .png file in Step 5, what dimensions (inches) would the image be saved?

ggsave('hotel_booking_chart.png',width=7,height=7)

A. 5x5

B. 7x7

C. 10x10

D. 25x25

The correct answer is B. 7x7. Explain: The dimensions you put in ggsave() image were 7x7. You can see these dimensions listed after you run the code chunk. Going forward, you can add annotations to enhance and clarify your visualizations with axis labels, chart titles, and more. You can then save images of your visualizations to share in reports and presentations.

Test your knowledge on annotating and saving visualizations

Question 1

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

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

Explain: 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.

Question 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?

A. The geom_bar() function

B. The facet_wrap() function

C. The labs() function

D. The geom_point() function

The correct answer is C. The labs() function

Question 3

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

A. The annotate() function

B. The labs() function

C. The facet_wrap() function

D. The aes() function

The correct answer is A. The annotate() function. Explain: You can use the annotate() function to put a text label inside the grid of your plot to call out specific data points.

Question 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("Penguins")

Output:

image

Where does your visualization display the title?

A. The upper right

B. The lower left

C. The upper left

D. The lower right

The correct answer is C. The upper left. Explain: 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.