Book Image

ggplot2 Essentials

By : Donato Teutonico
Book Image

ggplot2 Essentials

By: Donato Teutonico

Overview of this book

Table of Contents (14 chapters)
ggplot2 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

As a very powerful open source language, R is rapidly becoming a standard in the scientific community, particularly for data analysis and data visualization. This is related mainly to the vast availability of library packages, which empower the user to apply this software in virtually any field of scientific research. In relation to data visualization, R provides a rich palette of tools, and among the packages available, ggplot2 is fast becoming one of the more sophisticated and advanced packages. Its use is constantly growing in the community of R users. This increasing interest is particularly related to the ggplot2 capability of creating high-quality plots with a very appealing layout and coding that is sufficiently easy to use.

As a scripting language, R may be difficult to master, but in this book, you will find a large number of examples and tips as well as detailed explanations, which will provide you with all the necessary tools to understand the concepts behind ggplot2 in depth and concretely apply them to solve everyday problems related to data visualization. You will see step-by-step descriptions, ranging from the basic applications of ggplot2 in realizing simple plots up to the realization of more advanced and sophisticated plots. We will also dig into controlling plot details, which will enable you to perform a full customization of the plot you intend to realize. Finally, we will also see more special applications of ggplot2, for instance, regarding how to include map data in plots, realize heatmaps, and realize matrix scatterplots using additional packages based on ggplot2.

By the end of this book, you will not only have learned how to use the full potential of ggplot2, but you will also be able to generate publication-quality plots. Moreover, you will also be able to use this book and its examples as a reference for daily questions concerning the use of ggplot2 for data representation.

What this book covers

Chapter 1, Graphics in R, gets you up and running with R and ggplot2 by providing you with a description of the R installation as well as the integrated development environment you may want to use in your daily use of R. You will also be introduced to the general ideas behind the different packages available for data visualization, graphics and lattice, and you will also see a few examples of how different plots realized with these packages can be realized with ggplot2. This will provide you with a quick reference to the similarities and differences among the different versions of code if you are already familiar with the other packages.

Chapter 2, Getting Started, provides you with an easy start to ggplot2 and the different plots that can be realized. We will see different plot examples using the qplot (quick plot) function, which provides you with a simplified tool to easily generate plots. In this chapter, we will go through histograms, density plots, bar charts, boxplots, scatterplots, time series, and dot charts.

Chapter 3, The Layers and Grammar of Graphics, gives you a general introduction to the different components on which the grammar of graphics is based. Such components are, for instance, the scales, coordinate system, faceting, aesthetic, and geometry. Understanding these is crucial for the use of the full potential of this package since only understanding the layered grammar can provide you with the right overview of how the different components are connected to each other. In this chapter, you will also find a series of tables summarizing the options available for function arguments, such as geometry and statistics, and they are intended to provide you with a reference for consultation when needed.

Chapter 4, Advanced Plotting Techniques, presents a few more advanced features and plots that can be realized in ggplot2. Building on the knowledge you will have developed, we will see how the grammar components can be combined to generate more complex plots. You will see how more advanced aesthetic mapping can be realized, for instance, using variables generated from statistical analysis or how components such as text and reference lines can be included on a plot. We will then also see how polar coordinate systems can be used to generate pie charts and other types of plots.

Chapter 5, Controlling Plot Details, shows you how you can modify the plot details as well as the default plot layout. You will see how we can modify the plot title, axis, and legend appearance and position. Moreover, you will also see how themes can be used to completely customize the plot appearance without affecting the data represented.

Chapter 6, Plot Output, shows you how to modify and organize multiple plots after their realization. We will see how to reproduce multiple plots next to each other and save the plots in different file formats from the R console, as well as using scripting commands.

Chapter 7, Special Applications of ggplot2, shows you examples of the special application of ggplot2 and other packages based on ggplot2. We will see how we can include maps in plots as well as add data to such maps; we will see how we can draw scatterplot matrices to represent the relationships between different variables. Finally, we will see how we can realize heat maps.

What you need for this book

To get the most out of this book, you will need to install R on your computer, as well as the ggplot2 package. Additional packages, such as ggmap and GGally, will also be used in Chapter 7, Special Applications of ggplot2. In Chapter 1, Graphics in R, you will find a detailed description of how you can install R and additional packages on your computer. You will also find additional information concerning additional programming interfaces such as RStudio.

Who this book is for

This book is perfect for R programmers who are interested in learning to use ggplot2 for a different type of data visualization—from the basics up to using more advanced applications, such as faceting and grouping. Since this book will not cover the basics of R commands and objects, you should have a basic understanding of the R language.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " The qplot (quick plot) function is a basic high-level function of ggplot2"

Any command-line input or output is written as follows:

plot(age~circumference, data=Orange)

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "For instance, the Animation and Documentary types have a much smaller sample size compared with the other categories"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/3529OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.