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

Preface

The open source statistical software, R, is one of the most popular choices among researchers from various fields. This software has the capability to produce high-quality graphics, and data visualization is one of the most important tasks in data science tracks. Through effective visualization, we can easily uncover the underlying pattern among variables without doing any sophisticated statistical analysis. In this cookbook, we have focused on graphical analysis using R in a very simple way with each independent example. We have covered the default R functionality along with more advanced visualization techniques such as lattice, ggplot2, and three-dimensional plots. Readers will not only learn the code to produce the graph but also learn why certain code has been written with specific examples.

What this book covers

Chapter 1, R Graphics, introduces the reader to the R graphic system, how R graphs work with default libraries, and also to the very recent revolution of lattice and ggplot2. Here, readers will get a flavor of what is going to be discussed in the subsequent chapters.

Chapter 2, Basic Graph Functions, introduces recipes for some basic types of graphs, useful in almost any kind of data analysis. We will go through all the steps to get you going from reading your data into R, making a first graph, tweaking it to suit your needs, and then saving and exporting it for use in presentations and publications.

Chapter 3, Beyond the Basics – Adjusting Key Parameters, looks more closely at various arguments to graph functions and their values, highlighting common pitfalls and workarounds. The par() function is explained with some useful examples, showing how to adjust colors, sizes, margins, and the styles of various graph elements such as points, lines, bars, axes, and titles. The subsequent chapters 3 to 9 cover the graph types introduced in the first two chapters in more detail.

Chapter 4, Creating Scatter Plots, has over a dozen recipes that cover scatter plots, some of the simplest and most commonly used types of graphs in data analysis. We will see how we can make more enhanced plots by adjusting various arguments and using some new functions.

Chapter 5, Creating Line Graphs and Time Series Charts, discusses some more intermediate to advanced recipes on customizing line graphs, improving and speeding up line graphs with multiple lines, processing dates to make time series charts, sparklines, and stock charts.

Chapter 6, Creating Bar, Dot, and Pie Charts, will show you how you can create many useful variations of bar graphs and dot plots by using only the base library functions. We will also look at a few recipes that address common criticisms of pie charts with some ways to make them more readable.

Chapter 7, Creating Histograms, enhances the basic histogram in R by changing the plotting mode and bins, in addition to style adjustments. We will also look at some advanced recipes that combine histograms with other types of graphs.

Chapter 8, Box and Whisker Plots, looks into various stylistic and structural adjustments to box plots. We will start by looking at some basic arguments to change individual aspects of a box plot and slowly move to more advanced recipes that involve the use of multiple function calls.

Chapter 9, Creating Heat Maps and Contour Plots, discusses various types of heat maps to visualize correlations, trends and multivariate data, and contour plots to show topographical information in various two- and three-dimensional ways.

Chapter 10, Creating Maps, builds on the introduction to visualizing data on geographical maps in the first chapter and covers recipes on plotting data from the World Bank, World Health Organization (WHO), Google Maps API, and some Geographical Information Systems (GIS).

Chapter 11, Data Visualization Using Lattice, contains various recipes to create the most common graphs using the lattice library. Lattice is one of the most popular data visualization libraries in R. This chapter contains 9 different recipes ranging from bar charts to distributional plots and empirical cumulative distribution.

Chapter 12, Data Visualization Using ggplot2, contains how we can create very high-quality data visualization using the concept of Grammar of Graphics. There are 8 different recipes to create the most common graphics. This chapter contains 1 special recipe where we discuss how to annotate a graph. The annotated graph contains an enormous amount of information. The recipe ranges from very basic graphics to advanced ones, where we show how we can incorporate layered graphs, such as a scatter plot embedded with the lowest and least square-fitted lines.

Chapter 13, Inspecting Large Datasets, contains recipes related to one of the newest concepts of finding patterns in large data through visualization. The recipes of this chapter show how to create nice graphs that tell us a story about the pattern of relationship among variables.

Chapter 14, Three-dimensional Visualizations, has most of its recipes centered on creating three-dimensional graphics ranging from scatter plots to density estimations. In this chapter, we also include how to create three-dimensional scatter plots with an estimated linear plane.

Chapter 15, Finalizing Graphs for Publications and Presentations, discusses some tricks and tips to add some polish to our graphs so that they can be used for publication and presentation. We will cover many important practical topics such as exported graph file formats, high resolution formats, vector formats such as PDF, SVG, and PS, mathematical and scientific notations, text descriptions, fonts, graph templates, and themes.

What you need for this book

The readers of this book need to have some knowledge on basic R programming along with basic knowledge on statistical analysis and graphics. Though most of the graphs discussed in this book are very common and easy to understand, there are some recipes where readers need to have statistical knowledge. All of the examples in this book have been created with R Version 3.0.2, so readers can reproduce all of the examples with this version or higher. However, it's possible that, if a reader uses a lower version, most of the examples will still run but not all.

Who this book is for

This recipe book is targeted at those reader groups who are already exposed to R programming and want to learn effective graphics with the power of R and its various libraries.

Conventions

In this book, you will find a number of styles of text 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: "We also set the plot title using the main argument."

A block of code is set as follows:

install.packages("openair")
library(openair)

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "By default, the data is in ascending order of total points scored by each player (as can be seen from the light to dark blue progression in the Total Points column)."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 for all Packt books you have purchased from your account at http://www.packtpub.com. 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.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.