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

Annotate

  • To add notes to a document or diagram to explain or comment upon it
  1. Title
ggplot(data=penguins) +
    geom_point(mapping=aes(x=flipper_length_mm, y=body_mass_g, color=species)) +
    labs(title="Palmer Penguins: Body Mass vs. Flipper Length")

  1. Subtitle
ggplot(data=penguins) +
    geom_point(mapping=aes(x=flipper_length_mm, y=body_mass_g, color=species)) +
    labs(title="Palmer Penguins: Body Mass vs. Flipper Length", 
         subtitle="Sample of Three Penguin Species")

  1. Caption
ggplot(data=penguins) +
    geom_point(mapping=aes(x=flipper_length_mm, y=body_mass_g, color=species)) +
    labs(title="Palmer Penguins: Body Mass vs. Flipper Length", 
         subtitle="Sample of Three Penguin Species",
         caption="Data collected by Dr. Kristen Gorman")

  1. Annotate
ggplot(data=penguins) +
    geom_point(mapping=aes(x=flipper_length_mm, y=body_mass_g, color=species)) +
    labs(title="Palmer Penguins: Body Mass vs. Flipper Length", 
         subtitle="Sample of Three Penguin Species",
         caption="Data collected by Dr. Kristen Gorman") +
    annotate("text", x=220, y=3500, label="The Gentoos are the largest", color="purple",
             fontface="bold", size=4.5, angle=25)

p <- ggplot(data=penguins) +
     geom_point(mapping=aes(x=flipper_length_mm, y=body_mass_g, color=species)) +
     labs(title="Palmer Penguins: Body Mass vs. Flipper Length", 
          subtitle="Sample of Three Penguin Species",
          caption="Data collected by Dr. Kristen Gorman")
  
p+annotate("text", x=220, y=3500, label="The Gentoos are the largest", color="purple")

Drawing arrows and shapes 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().

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.

Save

# Example
ggsave('hotel_booking_chart.png',
       width=16,
       height=8)

Test your knowledge on annotating and saving visualizations

TOTAL POINTS 4

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
  • Helping stakeholders quickly understand your plot
  • Choosing a geom for your plot
  • Indicating the main purpose of your plot

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.

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?

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

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

Question 3

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

  • The labs() function
  • The aes() function
  • The annotate() function
  • The facet_wrap() 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.

Question 4

You are working with the penguins dataset. You create a scatterplot.

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.

ggplot(data = penguins) +
    geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
    labs(title="Penguins")

Where does your visualization display the title?

  • The lower left
  • The upper left
  • The upper right
  • The lower right

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.