R Graphics Cookbook
Winston Chang
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/0636920023135/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
I started using R several years ago to analyze data I had collected for my research in graduate school. My motivation at first was to escape from the restrictive environments and canned analyses offered by statistical programs like SPSS. And even better, because its freely available, I didnt need to convince someone to buy me a copy of the softwarevery important for a poor graduate student! As I delved deeper into R, I discovered that it could also create excellent data graphics.
Each recipe in this book lists a problem and a solution. In most cases, the solutions I offer arent the only way to do things in R, but they are, in my opinion, the best way. One of the reasons for Rs popularity is that there are many available add-on packages, each of which provides some functionality for R. There are many packages for visualizing data in R, but this book primarily uses ggplot2. (Disclaimer: its now part of my job to do development on ggplot2. However, I wrote much of this book before I had any idea that I would start a job related to ggplot2.)
This book isnt meant to be a comprehensive manual of all the different ways of creating data visualizations in R, but hopefully it will help you figure out how to make the graphics you have in mind. Or, if youre not sure what you want to make, browsing its pages may give you some ideas about whats possible.
Recipes
This book is intended for readers who have at least a basic understanding of R. The recipes in this book will show you how to do specific tasks. Ive tried to use examples that are simple, so that you can understand how they work and transfer the solutions over to your own problems.
Software and Platform Notes
Most of the recipes here use the ggplot2 graphing package. Some of the recipes require the most recent version of ggplot2, 0.9.3, and this in turn requires a relatively recent version of R. You can always get the latest version of R from the main R project site.
Note
If you are not familiar with ggplot2, see for a brief introduction to the package.
Once youve installed R, you can install the necessary packages. In addition to ggplot2, youll also want to install the gcookbook package, which contains data sets for many of the examples in this book. To install them both, run:
install.packages
(
"ggplot2"
)
install.packages
(
"gcookbook"
)
You may be asked to choose a mirror site for CRAN, the Comprehensive R Archive Network. Any of the sites should work, but its a good idea to choose one close to you because it will likely be faster than one far away. Once youve installed the packages, run this in each R session in which you want to use ggplot2:
library
(
ggplot2
)
The recipes in this book will assume that youve already loaded ggplot2, so they wont show this line.
If you see an error like this, it means that you forgot to load ggplot2:
Error: could not find function "ggplot"
The major platforms for R are Mac OS X, Linux, and Windows, and all the recipes in this book should work on all of these platforms. There are some platform-specific differences when it comes to creating bitmap output files, and these differences are covered in .
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This icon signifies a tip, suggestion, or general note.
Caution
This icon indicates a warning or caution.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: R Graphics Cookbook by Winston Chang (OReilly). Copyright 2013 Winston Chang, 978-1-449-31695-2.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
Safari Books Online
Note
Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.