Use R!
Advisors:
Robert Gentleman • Kurt Hornik • Giovanni Parmigiani
For other titles published in this series, go to
http://www.springer.com/series/6991
Hadley Wickham
ggplot2
Elegant Graphics for Data Analysis
123
Hadley Wickham
Rice University
Department of Statistics
Houston, TX
77005-1827
USA
hadley@rice.edu
Series Editors
Robert Gentleman
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Avenue, N. M2-B876
Seattle, Washington 98109
USA
Kurt Hornik
Department of Statistik and Mathematik
Wirtschaftsuniversit
¨
at Wien Augasse 2-6
A-1090 Wien
Austria
Giovanni Parmigiani
The Sidney Kimmel Comprehensive Cancer
Center at Johns Hopkins University
550 North Broadway
Baltimore, MD 21205-2011
USA
ISBN 978-0-387-98140-6 e-ISBN 978-0-387-98141-3
DOI 10.1007/978-0-387-98141-3
Springer Dordrecht Heidelberg London New York
Library of Congress Control Number: 2009928510
c
Springer Science+Business Media, LLC 2009
All rights reserved. This work may not be translated or copied in whole or in part without the written
permission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York,
NY 10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in
connection with any form of information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed is forbidden.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
Printed on acid-free paper
Springer is part of Springer Science+Business Media (www.springer.com)
Contents
1Introduction............................................... 1
1.1 Welcome to ggplot2 ...................................... 1
1.2 Other resources ......................................... 2
1.3 What is the grammar of graphics? ......................... 3
1.4 How does ggplot2 fit in with other R graphics? ............. 4
1.5 About this book......................................... 5
1.6 Installation ............................................. 6
1.7 Acknowledgements ...................................... 6
2 Getting started with qplot ................................. 9
2.1 Introduction ............................................ 9
2.2 Datasets ............................................... 10
2.3 Basic use ............................................... 11
2.4 Colour, size, shape and other aesthetic attributes ............ 12
2.5 Plot geoms ............................................. 13
2.5.1 Adding a smoother to a plot ........................ 14
2.5.2 Boxplots and jittered points ........................ 16
2.5.3 Histogram and density plots ........................ 18
2.5.4 Bar charts ........................................ 20
2.5.5 Time series with line and path plots ................. 20
2.6 Faceting ................................................ 22
2.7 Other options ........................................... 23
2.8 Differences from plot ..................................... 26
3 Mastering the grammar ................................... 27
3.1 Introduction ............................................ 27
3.2 Fuel economy data....................................... 28
3.3 Building a scatterplot .................................... 29
3.4 A more complex plot ..................................... 34
3.5 Components of the layered grammar ....................... 35
3.5.1 Layers ........................................... 37
3.5.2 Scales ............................................ 37
3.5.3 Coordinate system ................................. 38
3.5.4 Faceting .......................................... 38
3.6 Data structures ......................................... 39
V
VI Contents
4 Build a plot layer by layer ................................. 41
4.1 Introduction ............................................ 41
4.2 Creating a plot .......................................... 42
4.3 Layers ................................................. 42
4.4 Data ................................................... 45
4.5 Aesthetic mappings ...................................... 46
4.5.1 Plots and layers ................................... 46
4.5.2 Setting vs. mapping ............................... 47
4.5.3 Grouping ......................................... 49
4.5.4 Matching aesthetics to graphic objects ............... 52
4.6 Geoms ................................................. 55
4.7 Stat ................................................... 55
4.8 Position adjustments ..................................... 59
4.9 Pulling it all together .................................... 59
4.9.1 Combining geoms and stats ......................... 60
4.9.2 Displaying precomputed statistics ................... 62
4.9.3 Varying aesthetics and data ........................ 62
5Toolbox................................................... 65
5.1 Introduction ............................................ 65
5.2 Overall layering strategy ................................. 66
5.3 Basic plot types ......................................... 66
5.4 Displaying distributions .................................. 68
5.5 Dealing with overplotting ................................. 72
5.6 Surface plots ............................................ 77
5.7 Drawing maps .......................................... 77
5.8 Revealing uncertainty .................................... 80
5.9 Statistical summaries .................................... 81
5.9.1 Individual summary functions ....................... 84
5.9.2 Single summary function ........................... 84
5.10 Annotating a plot ....................................... 85
5.11 Weighted data .......................................... 88
6 Scales, axes and legends ................................... 91
6.1 Introduction ............................................ 91
6.2 How scales work ......................................... 92
6.3 Usage .................................................. 93
6.4 Scale details ............................................ 96
6.4.1 Common arguments ............................... 96
6.4.2 Position scales .................................... 98
6.4.3 Colour ...........................................102
6.4.4 The manual discrete scale ..........................107
6.4.5 The identity scale .................................109
- 1
- 2
前往页