Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Graph annotation with ggplot


To produce a publication-quality data visualization, we often need to annotate the graph with various texts, symbols, or even shapes. In this recipe, we will learn how we can easily annotate an existing graph.

Getting ready

In this recipe, we will use the disA and disD variables from ggplotdata. Let's call ggplotdata for this recipe. We also need to call the grid and gridExtra libraries for this recipe.

How to do it...

In this recipe, we will execute the following annotation on an existing scatter plot. So, the whole procedure will be as follows:

  1. Create a scatter plot.

  2. Add customized text within the plot.

  3. Highlight a certain region to indicate extreme values.

  4. Draw a line segment with an arrow within the scatter plot to indicate a single extreme observation.

Now, we will implement each of the steps one by one:

library(grid)
library(gridExtra)
# creating scatter plot and print it
annotation_obj <- ggplot(data=ggplotdata,aes(x=disA,y=disD))+geom_point()
annotation_obj...