R Graphics Cookbook
Welcome
Preface
0.1
Recipes
0.2
Software and Platform Notes
0.3
Conventions Used in This Book
0.4
Using Code Examples
0.5
How to Contact Us
0.6
Acknowledgments
1
R Basics
1.1
Installing a Package
1.1.1
Problem
1.1.2
Solution
1.1.3
Discussion
1.2
Loading a Package
1.2.1
Problem
1.2.2
Solution
1.2.3
Discussion
1.3
Upgrading Packages
1.3.1
Problem
1.3.2
Solution
1.3.3
Discussion
1.4
Loading a Delimited Text Data File
1.4.1
Problem
1.4.2
Solution
1.4.3
Discussion
1.4.4
See Also
1.5
Loading Data from an Excel File
1.5.1
Problem
1.5.2
Solution
1.5.3
Discussion
1.5.4
See Also
1.6
Loading Data from SPSS/SAS/Stata Files
1.6.1
Problem
1.6.2
Solution
1.6.3
Discussion
1.6.4
See Also
1.7
Chaining Functions Together With
%>%
, the Pipe Operator
1.7.1
Problem
1.7.2
Solution
1.7.3
Discussion
1.7.4
See Also
2
Quickly Exploring Data
2.1
Creating a Scatter Plot
2.1.1
Problem
2.1.2
Solution
2.1.3
See Also
2.2
Creating a Line Graph
2.2.1
Problem
2.2.2
Solution
2.2.3
See Also
2.3
Creating a Bar Graph
2.3.1
Problem
2.3.2
Solution
2.3.3
See Also
2.4
Creating a Histogram
2.4.1
Problem
2.4.2
Solution
2.4.3
See Also
2.5
Creating a Box Plot
2.5.1
Problem
2.5.2
Solution
2.5.3
See Also
2.6
Plotting a Function Curve
2.6.1
Problem
2.6.2
Solution
2.6.3
See Also
3
Bar Graphs
3.1
Making a Basic Bar Graph
3.1.1
Problem
3.1.2
Solution
3.1.3
Discussion
3.1.4
See Also
3.2
Grouping Bars Together
3.2.1
Problem
3.2.2
Solution
3.2.3
Discussion
3.2.4
See Also
3.3
Making a Bar Graph of Counts
3.3.1
Problem
3.3.2
Solution
3.3.3
Discussion
3.3.4
See Also
3.4
Using Colors in a Bar Graph
3.4.1
Problem
3.4.2
Solution
3.4.3
Discussion
3.4.4
See Also
3.5
Coloring Negative and Positive Bars Differently
3.5.1
Problem
3.5.2
Solution
3.5.3
Discussion
3.5.4
See Also
3.6
Adjusting Bar Width and Spacing
3.6.1
Problem
3.6.2
Solution
3.6.3
Discussion
3.7
Making a Stacked Bar Graph
3.7.1
Problem
3.7.2
Solution
3.7.3
Discussion
3.7.4
See Also
3.8
Making a Proportional Stacked Bar Graph
3.8.1
Problem
3.8.2
Solution
3.8.3
Discussion
3.8.4
See Also
3.9
Adding Labels to a Bar Graph
3.9.1
Problem
3.9.2
Solution
3.9.3
Discussion
3.9.4
See Also
3.10
Making a Cleveland Dot Plot
3.10.1
Problem
3.10.2
Solution
3.10.3
Discussion
3.10.4
See Also
4
Line Graphs
4.1
Making a Basic Line Graph
4.1.1
Problem
4.1.2
Solution
4.1.3
Discussion
4.1.4
See Also
4.2
Adding Points to a Line Graph
4.2.1
Problem
4.2.2
Solution
4.2.3
Discussion
4.2.4
See Also
4.3
Making a Line Graph with Multiple Lines
4.3.1
Problem
4.3.2
Solution
4.3.3
Discussion
4.4
Changing the Appearance of Lines
4.4.1
Problem
4.4.2
Solution
4.4.3
Discussion
4.4.4
See Also
4.5
Changing the Appearance of Points
4.5.1
Problem
4.5.2
Solution
4.5.3
Discussion
4.5.4
See Also
4.6
Making a Graph with a Shaded Area
4.6.1
Problem
4.6.2
Solution
4.6.3
Discussion
4.6.4
See Also
4.7
Making a Stacked Area Graph
4.7.1
Problem
4.7.2
Solution
4.7.3
Discussion
4.7.4
See Also
4.8
Making a Proportional Stacked Area Graph
4.8.1
Problem
4.8.2
Solution
4.8.3
Discussion
4.8.4
See Also
4.9
Adding a Confidence Region
4.9.1
Problem
4.9.2
Solution
4.9.3
Discussion
5
Scatter Plots
5.1
Making a Basic Scatter Plot
5.1.1
Problem
5.1.2
Solution
5.1.3
Discussion
5.2
Grouping Points Together using Shapes or Colors
5.2.1
Problem
5.2.2
Solution
5.2.3
Discussion
5.2.4
See Also
5.3
Using Different Point Shapes
5.3.1
Problem
5.3.2
Solution
5.3.3
Discussion
5.3.4
See Also
5.4
Mapping a Continuous Variable to Color or Size
5.4.1
Problem
5.4.2
Solution
5.4.3
Discussion
5.4.4
See Also
5.5
Dealing with Overplotting
5.5.1
Problem
5.5.2
Solution
5.5.3
Discussion
5.5.4
See Also
5.6
Adding Fitted Regression Model Lines
5.6.1
Problem
5.6.2
Solution
5.6.3
Discussion
5.7
Adding Fitted Lines from an Existing Model
5.7.1
Problem
5.7.2
Solution
5.7.3
Discussion
5.8
Adding Fitted Lines from Multiple Existing Models
5.8.1
Problem
5.8.2
Solution
5.8.3
Discussion
5.9
Adding Annotations with Model Coefficients
5.9.1
Problem
5.9.2
Solution
5.9.3
Discussion
5.9.4
See Also
5.10
Adding Marginal Rugs to a Scatter Plot
5.10.1
Problem
5.10.2
Solution
5.10.3
Discussion
5.10.4
See Also
5.11
Labeling Points in a Scatter Plot
5.11.1
Problem
5.11.2
Solution
5.11.3
Discussion
5.11.4
See Also
5.12
Creating a Balloon Plot
5.12.1
Problem
5.12.2
Solution
5.12.3
Discussion
5.12.4
See Also
5.13
Making a Scatter Plot Matrix
5.13.1
Problem
5.13.2
Solution
5.13.3
Discussion
5.13.4
See Also
6
Summarized Data Distributions
6.1
Making a Basic Histogram
6.1.1
Problem
6.1.2
Solution
6.1.3
Discussion
6.1.4
See Also
6.2
Making Multiple Histograms from Grouped Data
6.2.1
Problem
6.2.2
Solution
6.2.3
Discussion
6.3
Making a Density Curve
6.3.1
Problem
6.3.2
Solution
6.3.3
Discussion
6.3.4
See Also
6.4
Making Multiple Density Curves from Grouped Data
6.4.1
Problem
6.4.2
Solution
6.4.3
Discussion
6.5
Making a Frequency Polygon
6.5.1
Problem
6.5.2
Solution
6.5.3
Discussion
6.5.4
See Also
6.6
Making a Basic Box Plot
6.6.1
Problem
6.6.2
Solution
6.6.3
Discussion
6.7
Adding Notches to a Box Plot
6.7.1
Problem
6.7.2
Solution
6.7.3
Discussion
6.8
Adding Means to a Box Plot
6.8.1
Problem
6.8.2
Solution
6.8.3
Discussion
6.9
Making a Violin Plot
6.9.1
Problem
6.9.2
Solution
6.9.3
Discussion
6.9.4
See Also
6.10
Making a Dot Plot
6.10.1
Problem
6.10.2
Solution
6.10.3
Discussion
6.10.4
See Also
6.11
Making Multiple Dot Plots for Grouped Data
6.11.1
Problem
6.11.2
Solution
6.11.3
Discussion
6.12
Making a Density Plot of Two-Dimensional Data
6.12.1
Problem
6.12.2
Solution
6.12.3
Discussion
6.12.4
See Also
7
Annotations
7.1
Adding Text Annotations
7.1.1
Problem
7.1.2
Solution
7.1.3
Discussion
7.1.4
See Also
7.2
Using Mathematical Expressions in Annotations
7.2.1
Problem
7.2.2
Solution
7.2.3
Discussion
7.2.4
See Also
7.3
Adding Lines
7.3.1
Problem
7.3.2
Solution
7.3.3
Discussion
7.3.4
See Also
7.4
Adding Line Segments and Arrows
7.4.1
Problem
7.4.2
Solution
7.4.3
Discussion
7.4.4
See Also
7.5
Adding a Shaded Rectangle
7.5.1
Problem
7.5.2
Solution
7.5.3
Discussion
7.6
Highlighting an Item
7.6.1
Problem
7.6.2
Solution
7.6.3
Discussion
7.6.4
See Also
7.7
Adding Error Bars
7.7.1
Problem
7.7.2
Solution
7.7.3
Discussion
7.7.4
See Also
7.8
Adding Annotations to Individual Facets
7.8.1
Problem
7.8.2
Solution
7.8.3
Discussion
7.8.4
See Also
8
Axes
8.1
Swapping X- and Y-Axes
8.1.1
Problem
8.1.2
Solution
8.1.3
Discussion
8.1.4
See Also
8.2
Setting the Range of a Continuous Axis
8.2.1
Problem
8.2.2
Solution
8.2.3
Discussion
8.3
Reversing a Continuous Axis
8.3.1
Problem
8.3.2
Solution
8.3.3
Discussion
8.3.4
See Also
8.4
Changing the Order of Items on a Categorical Axis
8.4.1
Problem
8.4.2
Solution
8.4.3
Discussion
8.4.4
See Also
8.5
Setting the Scaling Ratio of the X- and Y-Axes
8.5.1
Problem
8.5.2
Solution
8.5.3
Discussion
8.6
Setting the Positions of Tick Marks
8.6.1
Problem
8.6.2
Solution
8.6.3
Discussion
8.6.4
See Also
8.7
Removing Tick Marks and Labels
8.7.1
Problem
8.7.2
Solution
8.7.3
Discussion
8.8
Changing the Text of Tick Labels
8.8.1
Problem
8.8.2
Solution
8.8.3
Discussion
8.9
Changing the Appearance of Tick Labels
8.9.1
Problem
8.9.2
Solution
8.9.3
Discussion
8.9.4
See Also
8.10
Changing the Text of Axis Labels
8.10.1
Problem
8.10.2
Solution
8.10.3
Discussion
8.11
Removing Axis Labels
8.11.1
Problem
8.11.2
Solution
8.11.3
Discussion
8.12
Changing the Appearance of Axis Labels
8.12.1
Problem
8.12.2
Solution
8.12.3
Discussion
8.12.4
See Also
8.13
Showing Lines Along the Axes
8.13.1
Problem
8.13.2
Solution
8.13.3
Discussion
8.13.4
See Also
8.14
Using a Logarithmic Axis
8.14.1
Problem
8.14.2
Solution
8.14.3
Discussion
8.15
Adding Ticks for a Logarithmic Axis
8.15.1
Problem
8.15.2
Solution
8.15.3
Discussion
8.16
Making a Circular Plot
8.16.1
Problem
8.16.2
Solution
8.16.3
Discussion
8.16.4
See Also
8.17
Using Dates on an Axis
8.17.1
Problem
8.17.2
Solution
8.17.3
Discussion
8.17.4
See Also
8.18
Using Relative Times on an Axis
8.18.1
Problem
8.18.2
Solution
8.18.3
Discussion
8.18.4
See Also
9
Controlling the Overall Appearance of Graphs
9.1
Setting the Title of a Graph
9.1.1
Problem
9.1.2
Solution
9.1.3
Discussion
9.2
Changing the Appearance of Text
9.2.1
Problem
9.2.2
Solution
9.2.3
Discussion
9.3
Using Themes
9.3.1
Problem
9.3.2
Solution
9.3.3
Discussion
9.3.4
See Also
9.4
Changing the Appearance of Theme Elements
9.4.1
Problem
9.4.2
Solution
9.4.3
Discussion
9.5
Creating Your Own Themes
9.5.1
Problem
9.5.2
Solution
9.5.3
Discussion
9.5.4
See Also
9.6
Hiding Grid Lines
9.6.1
Problem
9.6.2
Solution
9.6.3
Discussion
10
Legends
10.1
Removing the Legend
10.1.1
Problem
10.1.2
Solution
10.1.3
Discussion
10.2
Changing the Position of a Legend
10.2.1
Problem
10.2.2
Solution
10.2.3
Discussion
10.3
Changing the Order of Items in a Legend
10.3.1
Problem
10.3.2
Solution
10.3.3
Discussion
10.3.4
See Also
10.4
Reversing the Order of Items in a Legend
10.4.1
Problem
10.4.2
Solution
10.4.3
Discussion
10.5
Changing a Legend Title
10.5.1
Problem
10.5.2
Solution
10.5.3
Discussion
10.6
Changing the Appearance of a Legend Title
10.6.1
Problem
10.6.2
Solution
10.6.3
Discussion
10.6.4
See Also
10.7
Removing a Legend Title
10.7.1
Problem
10.7.2
Solution
10.7.3
Discussion
10.8
Changing the Labels in a Legend
10.8.1
Problem
10.8.2
Solution
10.8.3
Discussion
10.9
Changing the Appearance of Legend Labels
10.9.1
Problem
10.9.2
Solution
10.9.3
Discussion
10.9.4
See Also
10.10
Using Labels with Multiple Lines of Text
10.10.1
Problem
10.10.2
Solution
10.10.3
Discussion
11
Facets
11.1
Splitting Data into Subplots with Facets
11.1.1
Problem
11.1.2
Solution
11.1.3
Discussion
11.2
Using Facets with Different Axes
11.2.1
Problem
11.2.2
Solution
11.2.3
Discussion
11.2.4
See Also
11.3
Changing the Text of Facet Labels
11.3.1
Problem
11.3.2
Solution
11.3.3
Discussion
11.3.4
See Also
11.4
Changing the Appearance of Facet Labels and Headers
11.4.1
Problem
11.4.2
Solution
11.4.3
Discussion
11.4.4
See Also
12
Using Colors in Plots
12.1
Setting the Colors of Objects
12.1.1
Problem
12.1.2
Solution
12.1.3
Discussion
12.1.4
See Also
12.2
Representing Variables with Colors
12.2.1
Problem
12.2.2
Solution
12.2.3
Discussion
12.2.4
See Also
12.3
Using a Colorblind-Friendly Palette
12.3.1
Problem
12.3.2
Solution
12.3.3
Discussion
12.3.4
See Also
12.4
Using a Different Palette for a Discrete Variable
12.4.1
Problem
12.4.2
Solution
12.4.3
Discussion
12.4.4
See Also
12.5
Using a Manually Defined Palette for a Discrete Variable
12.5.1
Problem
12.5.2
Solution
12.5.3
Discussion
12.5.4
See Also
12.6
Using a Manually Defined Palette for a Continuous Variable
12.6.1
Problem
12.6.2
Solution
12.6.3
Discussion
12.6.4
See Also
12.7
Coloring a Shaded Region Based on Value
12.7.1
Problem
12.7.2
Solution
12.7.3
Discussion
13
Miscellaneous Graphs
13.1
Making a Correlation Matrix
13.1.1
Problem
13.1.2
Solution
13.1.3
Discussion
13.1.4
See Also
13.2
Plotting a Function
13.2.1
Problem
13.2.2
Solution
13.2.3
Discussion
13.2.4
See Also
13.3
Shading a Subregion Under a Function Curve
13.3.1
Problem
13.3.2
Solution
13.3.3
Discussion
13.4
Creating a Network Graph
13.4.1
Problem
13.4.2
Solution
13.4.3
Discussion
13.4.4
See Also
13.5
Using Text Labels in a Network Graph
13.5.1
Problem
13.5.2
Solution
13.5.3
Discussion
13.5.4
See Also
13.6
Creating a Heat Map
13.6.1
Problem
13.6.2
Solution
13.6.3
Discussion
13.6.4
See Also
13.7
Creating a Three-Dimensional Scatter Plot
13.7.1
Problem
13.7.2
Solution
13.7.3
Discussion
13.7.4
See Also
13.8
Adding a Prediction Surface to a Three-Dimensional Plot
13.8.1
Problem
13.8.2
Solution
13.8.3
Discussion
13.8.4
See Also
13.9
Saving a Three-Dimensional Plot
13.9.1
Problem
13.9.2
Solution
13.9.3
Discussion
13.10
Animating a Three-Dimensional Plot
13.10.1
Problem
13.10.2
Solution
13.10.3
Discussion
13.11
Creating a Dendrogram
13.11.1
Problem
13.11.2
Solution
13.11.3
Discussion
13.11.4
See Also
13.12
Creating a Vector Field
13.12.1
Problem
13.12.2
Solution
13.12.3
Discussion
13.12.4
See Also
13.13
Creating a QQ Plot
13.13.1
Problem
13.13.2
Solution
13.13.3
Discussion
13.13.4
See Also
13.14
Creating a Graph of an Empirical Cumulative Distribution Function
13.14.1
Problem
13.14.2
Solution
13.14.3
Discussion
13.15
Creating a Mosaic Plot
13.15.1
Problem
13.15.2
Solution
13.15.3
Discussion
13.15.4
See Also
13.16
Creating a Pie Chart
13.16.1
Problem
13.16.2
Solution
13.16.3
Discussion
13.17
Creating a Map
13.17.1
Problem
13.17.2
Solution
13.17.3
Discussion
13.17.4
See Also
13.18
Creating a Choropleth Map
13.18.1
Problem
13.18.2
Solution
13.18.3
Discussion
13.18.4
See Also
13.19
Making a Map with a Clean Background
13.19.1
Problem
13.19.2
Solution
13.19.3
Discussion
13.20
Creating a Map from a Shapefile
13.20.1
Problem
13.20.2
Solution
13.20.3
Discussion
13.20.4
See Also
14
Output for Presentation
14.1
Outputting to PDF Vector Files
14.1.1
Problem
14.1.2
Solution
14.1.3
Discussion
14.1.4
See Also
14.2
Outputting to SVG Vector Files
14.2.1
Problem
14.2.2
Solution
14.2.3
Discussion
14.3
Outputting to WMF Vector Files
14.3.1
Problem
14.3.2
Solution
14.3.3
Discussion
14.4
Editing a Vector Output File
14.4.1
Problem
14.4.2
Solution
14.4.3
Discussion
14.5
Outputting to Bitmap (PNG/TIFF) Files
14.5.1
Problem
14.5.2
Solution
14.5.3
Discussion
14.6
Using Fonts in PDF Files
14.6.1
Problem
14.6.2
Solution
14.6.3
Discussion
14.6.4
See Also
14.7
Using Fonts in Windows Bitmap or Screen Output
14.7.1
Problem
14.7.2
Solution
14.7.3
Discussion
14.8
Combining Several Plots into the Same Graphic
14.8.1
Problem
14.8.2
Solution
14.8.3
Discussion
14.8.4
See Also
15
Getting Your Data into Shape
15.1
Creating a Data Frame
15.1.1
Problem
15.1.2
Solution
15.1.3
Discussion
15.2
Getting Information About a Data Structure
15.2.1
Problem
15.2.2
Solution
15.2.3
Discussion
15.3
Adding a Column to a Data Frame
15.3.1
Problem
15.3.2
Solution
15.3.3
Discussion
15.4
Deleting a Column from a Data Frame
15.4.1
Problem
15.4.2
Solution
15.4.3
Discussion
15.4.4
See Also
15.5
Renaming Columns in a Data Frame
15.5.1
Problem
15.5.2
Solution
15.5.3
Discussion
15.5.4
See Also
15.6
Reordering Columns in a Data Frame
15.6.1
Problem
15.6.2
Solution
15.6.3
Discussion
15.7
Getting a Subset of a Data Frame
15.7.1
Problem
15.7.2
Solution
15.7.3
Discussion
15.8
Changing the Order of Factor Levels
15.8.1
Problem
15.8.2
Solution
15.8.3
Discussion
15.8.4
See Also
15.9
Changing the Order of Factor Levels Based on Data Values
15.9.1
Problem
15.9.2
Solution
15.9.3
Discussion
15.9.4
See Also
15.10
Changing the Names of Factor Levels
15.10.1
Problem
15.10.2
Solution
15.10.3
Discussion
15.10.4
See Also
15.11
Removing Unused Levels from a Factor
15.11.1
Problem
15.11.2
Solution
15.11.3
Discussion
15.12
Changing the Names of Items in a Character Vector
15.12.1
Problem
15.12.2
Solution
15.12.3
Discussion
15.12.4
See Also
15.13
Recoding a Categorical Variable to Another Categorical Variable
15.13.1
Problem
15.13.2
Solution
15.13.3
Discussion
15.13.4
See Also
15.14
Recoding a Continuous Variable to a Categorical Variable
15.14.1
Problem
15.14.2
Solution
15.14.3
Discussion
15.14.4
See Also
15.15
Calculating New Columns From Existing Columns
15.15.1
Problem
15.15.2
Solution
15.15.3
Discussion
15.15.4
See Also
15.16
Calculating New Columns by Groups
15.16.1
Problem
15.16.2
Solution
15.16.3
Discussion
15.16.4
See Also
15.17
Summarizing Data by Groups
15.17.1
Problem
15.17.2
Solution
15.17.3
Discussion
15.17.4
See Also
15.18
Summarizing Data with Standard Errors and Confidence Intervals
15.18.1
Problem
15.18.2
Solution
15.18.3
Discussion
15.18.4
See Also
15.19
Converting Data from Wide to Long
15.19.1
Problem
15.19.2
Solution
15.19.3
Discussion
15.19.4
See Also
15.20
Converting Data from Long to Wide
15.20.1
Problem
15.20.2
Solution
15.20.3
Discussion
15.20.4
See Also
15.21
Converting a Time Series Object to Times and Values
15.21.1
Problem
15.21.2
Solution
15.21.3
Discussion
15.21.4
See Also
Appendix
A
Understanding ggplot2
A.1
Background
A.2
Some Terminology and Theory
A.3
Building a Simple Plot
A.4
Printing
A.5
Stats
A.6
Themes
A.7
End
R Graphics Cookbook, 2nd edition
0.5
How to Contact Us