R Graphics Cookbook
Practical Recipes for Visualizing Data
Publisher: O'Reilly Media
Release Date: December 2012
Pages: 416
Read on Safari with a 10day trial
Start your free trial now Buy on AmazonWhere’s the cart? Now you can get everything on Safari. To purchase books, visit Amazon or your favorite retailer. Questions? See our FAQ or contact customer service:
18008898969 / 7078277019
support@oreilly.com
This practical guide provides more than 150 recipes to help you generate highquality graphs quickly, without having to comb through all the details of R’s graphing systems. Each recipe tackles a specific problem with a solution you can apply to your own project, and includes a discussion of how and why the recipe works.
Most of the recipes use the ggplot2 package, a powerful and flexible way to make graphs in R. If you have a basic understanding of the R language, you’re ready to get started.
 Use R’s default graphics for quick exploration of data
 Create a variety of bar graphs, line graphs, and scatter plots
 Summarize data distributions with histograms, density curves, box plots, and other examples
 Provide annotations to help viewers interpret data
 Control the overall appearance of graphics
 Render data groups alongside each other for easy comparison
 Use colors in plots
 Create network graphs, heat maps, and 3D scatter plots
 Structure data for graphing
Table of Contents

Chapter 1 R Basics

Installing a Package

Loading a Package

Loading a Delimited Text Data File

Loading Data from an Excel File

Loading Data from an SPSS File


Chapter 2 Quickly Exploring Data

Creating a Scatter Plot

Creating a Line Graph

Creating a Bar Graph

Creating a Histogram

Creating a Box Plot

Plotting a Function Curve


Chapter 3 Bar Graphs

Making a Basic Bar Graph

Grouping Bars Together

Making a Bar Graph of Counts

Using Colors in a Bar Graph

Coloring Negative and Positive Bars Differently

Adjusting Bar Width and Spacing

Making a Stacked Bar Graph

Making a Proportional Stacked Bar Graph

Adding Labels to a Bar Graph

Making a Cleveland Dot Plot


Chapter 4 Line Graphs

Making a Basic Line Graph

Adding Points to a Line Graph

Making a Line Graph with Multiple Lines

Changing the Appearance of Lines

Changing the Appearance of Points

Making a Graph with a Shaded Area

Making a Stacked Area Graph

Making a Proportional Stacked Area Graph

Adding a Confidence Region


Chapter 5 Scatter Plots

Making a Basic Scatter Plot

Grouping Data Points by a Variable Using Shape or Color

Using Different Point Shapes

Mapping a Continuous Variable to Color or Size

Dealing with Overplotting

Adding Fitted Regression Model Lines

Adding Fitted Lines from an Existing Model

Adding Fitted Lines from Multiple Existing Models

Adding Annotations with Model Coefficients

Adding Marginal Rugs to a Scatter Plot

Labeling Points in a Scatter Plot

Creating a Balloon Plot

Making a Scatter Plot Matrix


Chapter 6 Summarized Data Distributions

Making a Basic Histogram

Making Multiple Histograms from Grouped Data

Making a Density Curve

Making Multiple Density Curves from Grouped Data

Making a Frequency Polygon

Making a Basic Box Plot

Adding Notches to a Box Plot

Adding Means to a Box Plot

Making a Violin Plot

Making a Dot Plot

Making Multiple Dot Plots for Grouped Data

Making a Density Plot of TwoDimensional Data


Chapter 7 Annotations

Adding Text Annotations

Using Mathematical Expressions in Annotations

Adding Lines

Adding Line Segments and Arrows

Adding a Shaded Rectangle

Highlighting an Item

Adding Error Bars

Adding Annotations to Individual Facets


Chapter 8 Axes

Swapping X and YAxes

Setting the Range of a Continuous Axis

Reversing a Continuous Axis

Changing the Order of Items on a Categorical Axis

Setting the Scaling Ratio of the X and YAxes

Setting the Positions of Tick Marks

Removing Tick Marks and Labels

Changing the Text of Tick Labels

Changing the Appearance of Tick Labels

Changing the Text of Axis Labels

Removing Axis Labels

Changing the Appearance of Axis Labels

Showing Lines Along the Axes

Using a Logarithmic Axis

Adding Ticks for a Logarithmic Axis

Making a Circular Graph

Using Dates on an Axis

Using Relative Times on an Axis


Chapter 9 Controlling the Overall Appearance of Graphs

Setting the Title of a Graph

Changing the Appearance of Text

Using Themes

Changing the Appearance of Theme Elements

Creating Your Own Themes

Hiding Grid Lines


Chapter 10 Legends

Removing the Legend

Changing the Position of a Legend

Changing the Order of Items in a Legend

Reversing the Order of Items in a Legend

Changing a Legend Title

Changing the Appearance of a Legend Title

Removing a Legend Title

Changing the Labels in a Legend

Changing the Appearance of Legend Labels

Using Labels with Multiple Lines of Text


Chapter 11 Facets

Splitting Data into Subplots with Facets

Using Facets with Different Axes

Changing the Text of Facet Labels

Changing the Appearance of Facet Labels and Headers


Chapter 12 Using Colors in Plots

Setting the Colors of Objects

Mapping Variables to Colors

Using a Different Palette for a Discrete Variable

Using a Manually Defined Palette for a Discrete Variable

Using a ColorblindFriendly Palette

Using a Manually Defined Palette for a Continuous Variable

Coloring a Shaded Region Based on Value


Chapter 13 Miscellaneous Graphs

Making a Correlation Matrix

Plotting a Function

Shading a Subregion Under a Function Curve

Creating a Network Graph

Using Text Labels in a Network Graph

Creating a Heat Map

Creating a ThreeDimensional Scatter Plot

Adding a Prediction Surface to a ThreeDimensional Plot

Saving a ThreeDimensional Plot

Animating a ThreeDimensional Plot

Creating a Dendrogram

Creating a Vector Field

Creating a QQ Plot

Creating a Graph of an Empirical Cumulative Distribution Function

Creating a Mosaic Plot

Creating a Pie Chart

Creating a Map

Creating a Choropleth Map

Making a Map with a Clean Background

Creating a Map from a Shapefile


Chapter 14 Output for Presentation

Outputting to PDF Vector Files

Outputting to SVG Vector Files

Outputting to WMF Vector Files

Editing a Vector Output File

Outputting to Bitmap (PNG/TIFF) Files

Using Fonts in PDF Files

Using Fonts in Windows Bitmap or Screen Output


Chapter 15 Getting Your Data into Shape

Creating a Data Frame

Getting Information About a Data Structure

Adding a Column to a Data Frame

Deleting a Column from a Data Frame

Renaming Columns in a Data Frame

Reordering Columns in a Data Frame

Getting a Subset of a Data Frame

Changing the Order of Factor Levels

Changing the Order of Factor Levels Based on Data Values

Changing the Names of Factor Levels

Removing Unused Levels from a Factor

Changing the Names of Items in a Character Vector

Recoding a Categorical Variable to Another Categorical Variable

Recoding a Continuous Variable to a Categorical Variable

Transforming Variables

Transforming Variables by Group

Summarizing Data by Groups

Summarizing Data with Standard Errors and Confidence Intervals

Converting Data from Wide to Long

Converting Data from Long to Wide

Converting a Time Series Object to Times and Values


Appendix Introduction to ggplot2

Background


Colophon