Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo
ggplot2 Essentials

Book Description

Explore the full range of ggplot2 plotting capabilities to create meaningful and spectacular graphs

In Detail

ggplot2 is one of the most sophisticated and advanced packages of R and its use is constantly growing in the community of R programmers. This book is the perfect starting point for your journey in learning about one of the most refined and widely used plotting tools—ggplot2.

You will start by taking a tour of the most relevant packages available in R to create plots, such as graphics, lattice, and ggplot2. Following this, you will take a deep dive into the concepts of ggplot2 by covering different kinds of plots that will help you gear up for your first hands-on project. After this first taste of the capabilities of ggplot2, you will explore in detail the visualization approach implemented in this package, the so-called grammar of graphics, providing you with the basics to understand the backstage concepts in the plotting functions. Finally, you will take a look at some specialized and sophisticated applications of ggplot2, such as how to realize a complex scatterplot matrix, heatmaps, and how to make spatial visualization graphs.

What You Will Learn

  • Familiarize yourself with some important data visualization packages in R such as graphics, lattice, and ggplot2

  • Realize different kinds of simple plots with the basic qplot function

  • Understand the basics of the grammar of graphics, the data visualization approach implemented in ggplot2

  • Master the ggplot2 package in realizing complex and more advanced graphs

  • Personalize the graphical details and learn the aesthetics of plotting graphs

  • Save and export your plots in different formats

  • Include maps in ggplot graphs, overlay data on maps, and learn how to realize complex matrix scatterplots

  • 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 If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. ggplot2 Essentials
      1. Table of Contents
      2. ggplot2 Essentials
      3. Credits
      4. About the Author
      5. About the Reviewers
        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. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Graphics in R
        1. Getting ggplot2 up and running
          1. Packages in R
        2. The Integrated Development Environment
          1. RStudio
          2. Eclipse and StatET
          3. Emacs and ESS
        3. The plotting environments in R
        4. Standard graphics and grid-based graphics
        5. Graphics and standard plots
          1. Scatterplots with individual data points
          2. Scatterplots with the line of one tree
          3. Scatterplots with the line and points of one tree
          4. Boxplots of the orange dataset
          5. Boxplots with individual observations
          6. Histograms of the orange dataset
          7. Histograms with the reference line at the median value in red
        6. Lattice and Trellis plots
          1. Scatterplots with individual observations
          2. Scatterplots of the orange dataset with faceting
          3. Faceting scatterplots with line and points
          4. Scatterplots with grouping data
          5. Boxplots of the orange dataset
          6. Histograms of the orange dataset
          7. Histograms with the reference line at the median value in red
        7. ggplot2 and the grammar of graphics
        8. Further reading
        9. Summary
      9. 2. Getting Started
        1. General aspects
          1. Introduction to aesthetic attributes
          2. Introduction to faceting
        2. Histograms and density plots
        3. Bar charts
        4. Boxplots
        5. Scatterplots
        6. Time series
        7. Bubble charts and dot plots
        8. Further reading
        9. Summary
      10. 3. The Layers and Grammar of Graphics
        1. Components of layered grammar
        2. Creating a plot layer by layer with the ggplot function
        3. Scales
        4. The coordinate system
        5. Faceting
          1. Grid faceting
          2. Wrap faceting
        6. Layers in ggplot2
          1. Data
          2. Aesthetic mapping
          3. Geometric
          4. Stat
          5. Position adjustment
            1. Position adjustment of categorical data
            2. Position adjustment of continuous data
        7. Equivalent coding between qplot and ggplot
          1. Histograms and density plots
          2. Bar charts
          3. Boxplots
          4. Scatterplots
        8. Further reading
        9. Summary
      11. 4. Advanced Plotting Techniques
        1. Adding statistics
          1. Smooth lines
          2. Linear regression
            1. Statistics with faceting
        2. Advanced aesthetic mapping
          1. Typical aesthetic mappings available in ggplot2
          2. Mapping the aesthetic to new stat variables
          3. Difference in mapping continuous and categorical variables
          4. Adding text and reference lines to plots
            1. Add text and reference lines with facets
        3. Plots with polar coordinates
          1. A pie chart
          2. A bullseye chart
          3. A coxcomb diagram
        4. Further reading
        5. Summary
      12. 5. Controlling Plot Details
        1. Exploring scales
        2. The plot title and axis labels
        3. Axis scales
          1. The discrete axis
          2. The continuous axis
          3. Axis transformations
        4. Legends
          1. The legend title
          2. Legend keys and key labels
        5. Themes
          1. Themes for the legend
          2. Themes for the axis and title
          3. Themes for the plot background
          4. Themes with facets
        6. Further reading
        7. Summary
      13. 6. Plot Output
        1. Multiple plots in one page
          1. Arranging plots in rows and columns
          2. Specifying the plot position
        2. Saving plots to a file
          1. Saving the plot manually
          2. Saving the plot to a file without rendering it
            1. Saving a single plot
            2. Saving multiple plots on the same PDF file
          3. Saving the plot after rendering it
        3. Further reading
        4. Summary
      14. 7. Special Applications of ggplot2
        1. Plotting maps with ggplot2 and ggmap
          1. Mapping representations with ggplot2 and maps
          2. Representing maps with ggmap
        2. Scatterplot matrix with GGally
        3. Plotting heatmaps with ggplot2
        4. Further reading
        5. Summary
      15. Index