实例介绍
ggplot2基于Leland Wilkinson在Grammar of Graphics(图形的语法)中提出的理论,取首字母缩写再加上plot,于是得名ggplot。按照《图形的语法》一书中的观点,一张统计图形就是从数据到点、线或方块等几何对象的颜色、形状或大小等图形属性的一个映射,其中还可能包含对数据进行统计变换(如求均值或方差),最后将这个映射绘制在一定的坐标系中就得到了我们需要的图形。图中可能还有分面,就是生成关于数据的不同子集的图形。
Hadley wickham ggplot2 Elegant Graphics for Data Analysis ringer Hadley wickham Rice university Department of Statistics Houston. tX 77005-1827 USA hadley @rice. edu Series editors Robert gentleman Kurt hornik Program in Computational Biology Department of statistik and mathematik Division of public health sciences Wirtschaftsuniversitat Wien augasse 2-6 Fred Hutchinson Cancer Research Center A-1090 Wien 1100 Fairview AvenueN. M2-B876 austria Seattle, Washington 98109 USA Giovanni parmigiana The sidney Kimmel Comprehensive Cancer Center at Johns Hopkins University 550 North broadway Baltimore. MD 21205-2011 USA ISBN978-0-387-98140-6 e-ISBN978-0-387-98141-3 DOI10.10079780-38798141-3 Springer dordrecht heidelberg london New York Library of Congress Control Number: 2009928510 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 SpringerispartofSpringerScience+businessMedia(www.springer.com) Contents 1 Introduction 1. 1 Welcome to ggplot 2 1.2 Other resources 1.3 What is the grammar of graphics? 1.4 How does ggplot2 fit in with other R graphIcs? 1.5 About this book 1.6 Installation 11234566 1.7 Acknowledgements 2 Getting started with plot 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 Contents 4 Build p yer by layer 4.1 Introduction 4.2 Creating a plot 42 4.3 Layers 42 4. 4 Dat 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 5 Toolbox 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 5.9.1 Individual summary functions 84 5.9.2 Single summary function 84 5.10 Annotating a plot 85 5.11 Weighted data 6 Scales, axes and legends 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 Contents VIL 6.5 Legends and axes 110 6.6 More resources 112 7 Positioning 115 7.1 Introduction 7.2 Faceting ....115 7.2.1 Facet grid 116 7. 2.2 Facet wrap 120 7.2.3 Controlling scale 121 7.2.4 Missing faceting variables 123 7.2.5 Grouping vs faceting 123 7.2.6 Dodging vs. faceting 7.2.7 Continuous variables 125 129 7.3 Coordinate systems 129 7.3.1 Transformation 7.3.2 Statistics .133 7.3.3 Cartesian coordinate systems ..133 7.3.4 Non-Cartesian coordinate systems 136 8 Polishing your plots for publication .139 8.1 Themes .139 8.1.1 Built-in themes 140 8.1.2 Theme elements and element functions 142 8.2 Customising scales and geoms ..147 8.2.1 147 8.2.2 Geoms and stats 148 8.3 Saving your output 148 8.4 Multiple plots on the same page 151 8.4.1 Subplots 152 8.4.2 Rectangular grids ..153 9 Manipulating data∴ .157 9.1 An introduction to plyr 157 9. 1.1 Fitting multiple models 161 9.2 Converting data from wide to long 164 9.2.1 Multiple time series 164 9.2.2 Parallel coordinates plot 167 9.3 ggplot( meth 16 9.3.1 Linear models .170 9.3.2 Writing your own 173 10 Reducing duplication 177 10.1 Introduction .177 10.2 Iteration 177 10.3 Plot templates 178 10.4 Plot functions 181 VIII Contents A appendices a Translating between different syntaxes 185 A 1 Introduction A2 Translating between gplot and ggplot 185 ..185 A 2.1 Aesthetics 185 A.2.2L 186 A.2.3 Scales and axes 186 A 2.4 Plot options 187 A3 Base graphics 187 A.3.1 High-level plotting commands ..187 A.3.2 Low-level drawing 189 A.3.3 Legends, axes and grid lines 190 A 3.4 Colour palettes .190 A.3.5 Graphical parameters 190 A4 Lattice graphics 190 A.5 GPL 192 B Aesthetic specifications 195 B. 1 Colour .....195 B2 Line type 195 B3 Shape ....196 B 4 Size 196 B 5 Justification 196 C Manipulating plot rendering with grid 199 C 1 Introduction 199 C2 Plot viewports 199 C3 Plot grabs 201 C4 Saving your work .201 References 203 Index ...207 R code index 211 Chapter 1 Introduction 1.1 Welcome to ggplot 2 ggplot2 is an R package for producing statistical, or data, graphics, but it is unlike most other graphics packages because it has a deep underlying grammar. This grammar, based on the Grammar of Graphics(Wilkinson 2005), is composed of a set of independent components that can be composed in many different ways. This makes ggplot2 very powerful, because you are not limited to a set of pre-specified graphics, but you can create new graphics that are precisely tailored for your problem. This may sound overwhelming, but because there is a simple set of core principles and very few special cases ggplot2 is also easy to learn(although it may take a little time to forget your preconceptions from other graphics tools Practically, ggplot2 provides beautiful, hassle-free plots, that take care of fiddly details like drawing legends. The plots can be built up iteratively and dited later a carefully chosen set of defaults means that most of the time ou can produce a publication-quality graphic in seconds, but if you do have special formatting requirements, a comprehensive theming system makes it easy to do what you want. Instead of spending time making your graph look pretty, you can focus on creating a graph that best reveals the messages in your data ggplot2 is designed to work in a layered fashion, starting with a layer showing the raw data then adding layers of annotations and statistical sum- maries. It allows you to produce graphics using the same structured thinking that you use to design an analysis, reducing the distance between a plot in your head and one on the page. It is especially helpful for students who have not yet developed the structured approach to analysis used by experts Learning the grammar will help you not only create graphics that you know about now, but will also help you to think about new graphics that would be even better. Without the grammar, there is no underlying theory and existing graphics packages are just a big collection of special cases. For example, in base r, if you design a new graphic, it's composed of raw plot elements like H. Wickham, ggplot2,UseR,DOI10.1007/978-0-387-98141-3_1 Springer Science+Business Media, LLC 2009 1 Introduction points and lines, and it's hard to design new components that combine with existing plots. In ggplot2, the expressions used to create a new graphic are composed of higher-level elements like representations of the raw data and statistical transformations, and can easily be combined with new datasets and other plots This book provides a hands-on introduction to ggplot2 with lots of example code and graphics. It also explains the grammar on which ggplot2 is based Like other formal systems, ggplot2 is useful even when you dont understand the underlying model. However, the more you learn about it, the more effectively you'll be able to use ggplot2. This book assumes some basic familiarity with r to the level described in the first chapter of Dalgaard's Introductory statistics with R. You should know how to get your data into R and how to do basic data manipulations. If you dont, you might want to get a copy of Phil Spector's Data Manipulation with R This book will introduce you to ggplot2 as a novice, unfamiliar with the grammar; teach you the basics so that you can re-create plots you are Iready familiar with; show you how to use the grammar to create new types of graphics; and even turn you into an expert who can build new components to extend the grammar 1.2 Other resources This book teaches you the elements of ggplot2's grammar and how they fit together, but it does not document every function in complete detail Furthermore, ggplot2 will almost certainly continue to evolve. For these reasons, you will need additional documentation as your use of ggplot2 becomes more complex and varied he best resource for low-level details will always be the built-in documen tationThisisaccessibleonlinehttp://had.co.nz/ggplot2,andfromwithin R using the usual help syntax. The advantage of the online documentation is that you can see all the example plots and navigate between topics more easily. The website also lists talks and papers related to ggplot2 and training opportunitiesifyoudlikesomehands-onpracticeTheCranwebsitehttp //cranr-project. org/web/packages/ggplot2/, is another useful resource This page links to what is new and different in each release. If you use ggplot2regularlyit'sagoodideatosignupfortheggplot2mailinglisthttp //groups. google. com/group/ggplot2. The list has relatively low traffic and is very friendly to new users Finallythebookwebsitehttp://had.co.nz/ggplot2/book,provides updates to this book, as well as pdfs containing all graphics used in the book with the code and data needed to reproduce them 【实例截图】
【核心代码】
标签:
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论