You are previewing R Graphs Cookbook Second Edition.
O'Reilly logo
R Graphs Cookbook Second Edition

Book Description

Over 70 recipes for building and customizing publication-quality visualizations of powerful and stunning R graphs

In Detail

Data visualization is one of the most important tasks in the data science track. Through effective visualization, we can easily uncover underlying patterns among variables without doing any sophisticated statistical analysis.

Starting with a high-level overview of the R graphics system and then moving through this practical cookbook, you will leverage powerful default R graphics and utilize advanced graphics systems such as lattice and ggplot2, the grammar of graphics. Through inspecting large datasets using tableplot and stunning three-dimensional visualizations, you will know how to produce, customize, and publish advanced visualizations using this popular, and powerful, framework.

What You Will Learn

  • Create diverse types of bar charts using the default R functions
  • Produce and customize density plots and histograms with lattice and ggplot2
  • Visualize frequency tabulated data with interactive and beautiful graphs
  • Inspect large datasets by simultaneously visualizing numeric and categorical variables in a single plot
  • Construct multiple graph matrix layouts
  • Annotate graphs using ggplot2
  • Construct various types of three-dimensional plots using three-dimensional visualizations
  • Downloading the example code for this book. 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.

    Table of Contents

    1. R Graphs Cookbook Second Edition
      1. Table of Contents
      2. R Graphs Cookbook Second Edition
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. R Graphics
        1. Base graphics using the default package
        2. Trellis graphs using lattice
        3. Graphs inspired by Grammar of Graphics
      9. 2. Basic Graph Functions
        1. Introduction
        2. Creating basic scatter plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. A note on R's built-in datasets
          5. See also
        3. Creating line graphs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Creating bar charts
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Creating histograms and density plots
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        6. Creating box plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Adjusting x and y axes' limits
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        8. Creating heat maps
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        9. Creating pairs plots
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        10. Creating multiple plot matrix layouts
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        11. Adding and formatting legends
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        12. Creating graphs with maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        13. Saving and exporting graphs
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      10. 3. Beyond the Basics – Adjusting Key Parameters
        1. Introduction
        2. Setting colors of points, lines, and bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Setting plot background colors
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Setting colors for text elements – axis annotations, labels, plot titles, and legends
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Choosing color combinations and palettes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        6. Setting fonts for annotations and titles
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Choosing plotting point symbol styles and sizes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        8. Choosing line styles and width
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Choosing box styles
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Adjusting axis annotations and tick marks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        11. Formatting log axes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Setting graph margins and dimensions
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      11. 4. Creating Scatter Plots
        1. Introduction
        2. Grouping data points within a scatter plot
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Highlighting grouped data points by size and symbol type
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Labeling data points
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Correlation matrix using pairs plots
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Adding error bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Using jitter to distinguish closely packed data points
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Adding linear model lines
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Adding nonlinear model curves
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Adding nonparametric model curves with lowess
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Creating three-dimensional scatter plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Creating Quantile-Quantile plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        13. Displaying the data density on axes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        14. Creating scatter plots with a smoothed density representation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      12. 5. Creating Line Graphs and Time Series Charts
        1. Introduction
        2. Adding customized legends for multiple-line graphs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Using margin labels instead of legends for multiple-line graphs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Adding horizontal and vertical grid lines
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        5. Adding marker lines at specific x and y values using abline
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Creating sparklines
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Plotting functions of a variable in a dataset
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Formatting time series data for plotting
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Plotting the date or time variable on the x axis
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        10. Annotating axis labels in different human-readable time formats
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Adding vertical markers to indicate specific time events
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. Plotting data with varying time-averaging periods
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Creating stock charts
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      13. 6. Creating Bar, Dot, and Pie Charts
        1. Introduction
        2. Creating bar charts with more than one factor variable
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Creating stacked bar charts
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Adjusting the orientation of bars – horizontal and vertical
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Adjusting bar widths, spacing, colors, and borders
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Displaying values on top of or next to the bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Placing labels inside bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Creating bar charts with vertical error bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Modifying dot charts by grouping variables
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Making better, readable pie charts with clockwise-ordered slices
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Labeling a pie chart with percentage values for each slice
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        12. Adding a legend to a pie chart
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      14. 7. Creating Histograms
        1. Introduction
        2. Visualizing distributions as count frequencies or probability densities
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        3. Setting the bin size and the number of breaks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        4. Adjusting histogram styles – bar colors, borders, and axes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        5. Overlaying a density line over a histogram
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Multiple histograms along the diagonal of a pairs plot
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Histograms in the margins of line and scatter plots
          1. Getting ready
          2. How to do it...
          3. How it works...
      15. 8. Box and Whisker Plots
        1. Introduction
        2. Creating box plots with narrow boxes for a small number of variables
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        3. Grouping over a variable
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        4. Varying box widths by the number of observations
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating box plots with notches
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        6. Including or excluding outliers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Creating horizontal box plots
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Changing the box styling
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        9. Adjusting the extent of plot whiskers outside the box
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        10. Showing the number of observations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        11. Splitting a variable at arbitrary values into subsets
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
      16. 9. Creating Heat Maps and Contour Plots
        1. Introduction
        2. Creating heat maps of a single Z variable with a scale
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        3. Creating correlation heat maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        4. Summarizing multivariate data in a single heat map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        5. Creating contour plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        6. Creating filled contour plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        7. Creating three-dimensional surface plots
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        8. Visualizing time series as calendar heat maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
      17. 10. Creating Maps
        1. Introduction
        2. Plotting global data by countries on a world map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        3. Creating graphs with regional maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        4. Plotting data on Google maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        5. Creating and reading KML data
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See Also
        6. Working with ESRI shapefiles
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
      18. 11. Data Visualization Using Lattice
        1. Introduction
        2. Creating bar charts
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Creating stacked bar charts
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Creating bar charts to visualize cross-tabulation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Creating a conditional histogram
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Visualizing distributions through a kernel-density plot
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Creating a normal Q-Q plot
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Visualizing an empirical Cumulative Distribution Function
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Creating a boxplot
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Creating a conditional scatter plot
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      19. 12. Data Visualization Using ggplot2
        1. Introduction
        2. Creating bar charts
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Creating multiple bar charts
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Creating a bar chart with error bars
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Visualizing the density of a numeric variable
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more...
        6. Creating a box plot
          1. Getting ready
          2. How to do it...
          3. How it works…
        7. Creating a layered plot with a scatter plot and fitted line
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more...
        8. Creating a line chart
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more...
        9. Graph annotation with ggplot
          1. Getting ready
          2. How to do it...
          3. How it works...
      20. 13. Inspecting Large Datasets
        1. Introduction
        2. Multivariate continuous data visualization
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Multivariate categorical data visualization
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Visualizing mixed data
          1. Getting ready
          2. How to do it…
        5. Zooming and filtering
          1. Getting ready
          2. How to do it...
          3. How it works…
          4. There's more...
      21. 14. Three-dimensional Visualizations
        1. Introduction
        2. Three-dimensional scatter plots
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also...
        3. Three-dimensional scatter plots with a regression plane
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Three-dimensional bar charts
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Three-dimensional density plots
          1. Getting ready
          2. How to do it...
          3. How it works…
      22. 15. Finalizing Graphs for Publications and Presentations
        1. Introduction
        2. Exporting graphs in high-resolution image formats – PNG, JPEG, BMP, and TIFF
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        3. Exporting graphs in vector formats – SVG, PDF, and PS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        4. Adding mathematical and scientific notations (typesetting)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        5. Adding text descriptions to graphs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        6. Using graph templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
        7. Choosing font families and styles under Windows, Mac OS X, and Linux
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
          5. See also
        8. Choosing fonts for PostScripts and PDFs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more
      23. Index