R Graphics Cookbook

Book description

This practical guide provides more than 150 recipes to help you generate high-quality 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

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Recipes
    2. Software and Platform Notes
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  3. 1. R Basics
    1. Installing a Package
    2. Loading a Package
    3. Loading a Delimited Text Data File
    4. Loading Data from an Excel File
    5. Loading Data from an SPSS File
  4. 2. Quickly Exploring Data
    1. Creating a Scatter Plot
    2. Creating a Line Graph
    3. Creating a Bar Graph
    4. Creating a Histogram
    5. Creating a Box Plot
    6. Plotting a Function Curve
  5. 3. Bar Graphs
    1. Making a Basic Bar Graph
    2. Grouping Bars Together
    3. Making a Bar Graph of Counts
    4. Using Colors in a Bar Graph
    5. Coloring Negative and Positive Bars Differently
    6. Adjusting Bar Width and Spacing
    7. Making a Stacked Bar Graph
    8. Making a Proportional Stacked Bar Graph
    9. Adding Labels to a Bar Graph
    10. Making a Cleveland Dot Plot
  6. 4. Line Graphs
    1. Making a Basic Line Graph
    2. Adding Points to a Line Graph
    3. Making a Line Graph with Multiple Lines
    4. Changing the Appearance of Lines
    5. Changing the Appearance of Points
    6. Making a Graph with a Shaded Area
    7. Making a Stacked Area Graph
    8. Making a Proportional Stacked Area Graph
    9. Adding a Confidence Region
  7. 5. Scatter Plots
    1. Making a Basic Scatter Plot
    2. Grouping Data Points by a Variable Using Shape or Color
    3. Using Different Point Shapes
    4. Mapping a Continuous Variable to Color or Size
    5. Dealing with Overplotting
    6. Adding Fitted Regression Model Lines
    7. Adding Fitted Lines from an Existing Model
    8. Adding Fitted Lines from Multiple Existing Models
    9. Adding Annotations with Model Coefficients
    10. Adding Marginal Rugs to a Scatter Plot
    11. Labeling Points in a Scatter Plot
    12. Creating a Balloon Plot
    13. Making a Scatter Plot Matrix
  8. 6. Summarized Data Distributions
    1. Making a Basic Histogram
    2. Making Multiple Histograms from Grouped Data
    3. Making a Density Curve
    4. Making Multiple Density Curves from Grouped Data
    5. Making a Frequency Polygon
    6. Making a Basic Box Plot
    7. Adding Notches to a Box Plot
    8. Adding Means to a Box Plot
    9. Making a Violin Plot
    10. Making a Dot Plot
    11. Making Multiple Dot Plots for Grouped Data
    12. Making a Density Plot of Two-Dimensional Data
  9. 7. Annotations
    1. Adding Text Annotations
    2. Using Mathematical Expressions in Annotations
    3. Adding Lines
    4. Adding Line Segments and Arrows
    5. Adding a Shaded Rectangle
    6. Highlighting an Item
    7. Adding Error Bars
    8. Adding Annotations to Individual Facets
  10. 8. Axes
    1. Swapping X- and Y-Axes
    2. Setting the Range of a Continuous Axis
    3. Reversing a Continuous Axis
    4. Changing the Order of Items on a Categorical Axis
    5. Setting the Scaling Ratio of the X- and Y-Axes
    6. Setting the Positions of Tick Marks
    7. Removing Tick Marks and Labels
    8. Changing the Text of Tick Labels
    9. Changing the Appearance of Tick Labels
    10. Changing the Text of Axis Labels
    11. Removing Axis Labels
    12. Changing the Appearance of Axis Labels
    13. Showing Lines Along the Axes
    14. Using a Logarithmic Axis
    15. Adding Ticks for a Logarithmic Axis
    16. Making a Circular Graph
    17. Using Dates on an Axis
    18. Using Relative Times on an Axis
  11. 9. Controlling the Overall Appearance of Graphs
    1. Setting the Title of a Graph
    2. Changing the Appearance of Text
    3. Using Themes
    4. Changing the Appearance of Theme Elements
    5. Creating Your Own Themes
    6. Hiding Grid Lines
  12. 10. Legends
    1. Removing the Legend
    2. Changing the Position of a Legend
    3. Changing the Order of Items in a Legend
    4. Reversing the Order of Items in a Legend
    5. Changing a Legend Title
    6. Changing the Appearance of a Legend Title
    7. Removing a Legend Title
    8. Changing the Labels in a Legend
    9. Changing the Appearance of Legend Labels
    10. Using Labels with Multiple Lines of Text
  13. 11. Facets
    1. Splitting Data into Subplots with Facets
    2. Using Facets with Different Axes
    3. Changing the Text of Facet Labels
    4. Changing the Appearance of Facet Labels and Headers
  14. 12. Using Colors in Plots
    1. Setting the Colors of Objects
    2. Mapping Variables to Colors
    3. Using a Different Palette for a Discrete Variable
    4. Using a Manually Defined Palette for a Discrete Variable
    5. Using a Colorblind-Friendly Palette
    6. Using a Manually Defined Palette for a Continuous Variable
    7. Coloring a Shaded Region Based on Value
  15. 13. Miscellaneous Graphs
    1. Making a Correlation Matrix
    2. Plotting a Function
    3. Shading a Subregion Under a Function Curve
    4. Creating a Network Graph
    5. Using Text Labels in a Network Graph
    6. Creating a Heat Map
    7. Creating a Three-Dimensional Scatter Plot
    8. Adding a Prediction Surface to a Three-Dimensional Plot
    9. Saving a Three-Dimensional Plot
    10. Animating a Three-Dimensional Plot
    11. Creating a Dendrogram
    12. Creating a Vector Field
    13. Creating a QQ Plot
    14. Creating a Graph of an Empirical Cumulative Distribution Function
    15. Creating a Mosaic Plot
    16. Creating a Pie Chart
    17. Creating a Map
    18. Creating a Choropleth Map
    19. Making a Map with a Clean Background
    20. Creating a Map from a Shapefile
  16. 14. Output for Presentation
    1. Outputting to PDF Vector Files
    2. Outputting to SVG Vector Files
    3. Outputting to WMF Vector Files
    4. Editing a Vector Output File
    5. Outputting to Bitmap (PNG/TIFF) Files
    6. Using Fonts in PDF Files
    7. Using Fonts in Windows Bitmap or Screen Output
  17. 15. Getting Your Data into Shape
    1. Creating a Data Frame
    2. Getting Information About a Data Structure
    3. Adding a Column to a Data Frame
    4. Deleting a Column from a Data Frame
    5. Renaming Columns in a Data Frame
    6. Reordering Columns in a Data Frame
    7. Getting a Subset of a Data Frame
    8. Changing the Order of Factor Levels
    9. Changing the Order of Factor Levels Based on Data Values
    10. Changing the Names of Factor Levels
    11. Removing Unused Levels from a Factor
    12. Changing the Names of Items in a Character Vector
    13. Recoding a Categorical Variable to Another Categorical Variable
    14. Recoding a Continuous Variable to a Categorical Variable
    15. Transforming Variables
    16. Transforming Variables by Group
    17. Summarizing Data by Groups
    18. Summarizing Data with Standard Errors and Confidence Intervals
    19. Converting Data from Wide to Long
    20. Converting Data from Long to Wide
    21. Converting a Time Series Object to Times and Values
  18. A. Introduction to ggplot2
    1. Background
  19. Index
  20. About the Author
  21. Colophon
  22. Copyright

Product information

  • Title: R Graphics Cookbook
  • Author(s): Winston Chang
  • Release date: December 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449316952