RGraphicscookbook
[R.Graphics.Cookbook(2012.12)].Winston.Chang.R Graphics CookbookWinston Chang○ REILLYBeijing Cambridge. Farnham. Koln. Sebastopol TokyoR Graphics Cookbookby Winston ChangCopyright o 2013 Winston Chang. All rights reservedPrinted in the United States of AmericaPublished by o reilly media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA 95472OReilly books may be purchased for educational, business, or sales promotional use. Online editions arealsoavailableformosttitles(http://my.safaribooksonline.com).foRmoreinformationcontactourcorporateinstitutionalsalesdepartment800-998-9938orcorporate@oreilly.comEditors: Mike Loukides and Courtney nashProofreader: Jilly GagnonProduction Editor: Holly BauerIndexer: Lucie haskinsCopyeditor: Rachel HeadCover Designer: Randall ComerInterior Designer: David FutatoIllustrator rebecca demarest and robert romaneDecember 2012: First editionRevision history for the First Edition:2012-12-04 First releaseSeehttp://oreilly.com/catalog/errata.csp?isbn=9781449316952forreleasedetailsNutshell Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO ReillyMedia, InC R Graphics Cookbook, the image of a reindeer, and related trade dress are trademarks of O reillyMedia IncMany of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and o reilly media, Inc, was aware of a trademark claim, the designations have been printed in caps or initial capsWhile every precaution has been taken in the preparation of this book the publisher and authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information containedereinISBN:978-1-449-31695-2[CKITable of contentsPreface1. RB1. 1. Installing a Package1. 2. Loading a Package1.3. Loading a delimited Text Data File1.4. Loading Data from an Excel File41.5. Loading data from an SPSS File2. Quickly Exploring Data2. 1. Creating a Scatter Plot2. 2. Creating a Line graph2.3. Creating a Bar Graph112. 4. Creating a Histogram2.5. Creating a Box Plot152.6. Plotting a Function Curve3. Bar GraphS.....3. 1. Making a Basic Bar Graph193. 2. Grouping Bars Together223.3. Making a Bar Graph of Counts253.4. Using Colors in a Bar Graph273.5. Coloring Negative and Positive Bars Differently293.6. Adjusting Bar Width and Spacing03.7. Making a Stacked Bar Graph323.8. Making a Proportional Stacked Bar Graph353.9. Adding Labels to a Bar graph383.10. Making a Cleveland Dot plot424. Line Graphs..............·。·4.1. Making a Basic Line graph494. 2. Adding Points to a Line graph524.3. Making a Line Graph with Multiple Lines534.4. Changing the Appearance of Lines584.5. Changing the Appearance of Points594.6. Making a graph with a Shaded area4.7. Making a Stacked Area grap644.8. Making a Proportional Stacked Area Graph674.9. Adding a Confidence Region695. Scatter plots5.1. Making a Basic Scatter Plot735.2. Grouping Data Points by a Variable Using Shape or Color5.3. Using Different Point Shapes5.4. Mapping a Continuous Variable to Color or Size5.5. Dealing with Overplotting845.6. Adding Fitted Regression Model Lines895.7. Adding Fitted Lines from an Existing Model945.8. Adding Fitted Lines from Multiple Existing Models975.9. Adding Annotations with Model Coefficients1005.10. Adding Marginal Rugs to a Scatter Plot1035.11. Labeling Points in a Scatter Plot1045. 12. Creating a Balloon plot1105. 13. Making a Scatter Plot Matrix6. Summarized data distributions1176. 1. Making a Basic histogram1176.2. Making Multiple Histograms from Grouped Data1206.3. Making a Density Curv1236.4. Making multiple density Curves from Grouped data1266.5. Making a frequency polygon1296.6. Making a Basic Box Plot1306.7. Adding notches to a Box Plot1336.8. Adding means to a box plot1346.9. Making a Violin Plot1356.10. Making a Dot Plot1396. 11. Making Multiple Dot Plots for Grouped Data1416. 12. Making a Density Plot of Two-Dimensional Data1437. Annotations,1477.1. Adding Text Annotations1477. 2. Using Mathematical Expressions in Annotations150Table of contents7.3. Adding lines1527.4. Adding line segments and arrows1557.5. Adding a Shaded Rectangle1567.6. Highlighting an Item1577.7. Adding error bars1597.8. Adding Annotations to Individual Facets1628. Axes1678. 1. Swapping X-and Y-Axes1678.2. Setting the Range of a Continuous Axis1688.3. Reversing a Continuous axis1708.4. Changing the Order of Items on a Categorical Axis1728.5. Setting the Scaling Ratio of the X-and Y-Axes1748.6. Setting the Positions of Tick Marks1778.7. Removing Tick Marks and labels1788.8. Changing the Text of Tick Labels1808.9. Changing the Appearance of Tick Labels1828.10. Changing the Text of Axis Labels1848. 11. Removing Axis Labels1858.12. Changing the Appearance of Axis Labels1878. 13. Showing Lines Along the Axes1898. 14. USing a Logarithmic Axis1908.15. Adding Ticks for a Logarithmic Axis1968.16. Making a Circular Grapl1988. 17. Using dates on an axis2048.18. Using Relative Times on an Axis2079. Controlling the Overall Appearance of Graphs............2119.1. Setting the Title of a graph2119. 2. Changing the Appearance of Text2139.3. USing Themes2169.4. Changing the Appearance of Theme Elements2189.5. Creating Your Own Themes2219.6. Hiding Grid Lines22210. Legends22510.1. Removing the Legend22510.2. Changing the Position of a Legend22710.3. Changing the Order of Items in a Legend22910.4. Reversing the Order of Items in a Legend23110.5. Changing a Legend Title23210.6. Changing the Appearance of a Legend Title235Table of content10.7. Removing a Legend Title23610.8. Changing the labels in a legend23710.9. Changing the Appearance of Legend labels23910.10. Using Labels with Multiple Lines of Text24024311.1. Splitting Data into Subplots with Facets24311. 2. Using Facets with Different Axes246Changing the lext of facet Labels24611.4. Changing the Appearance of Facet Labels and Headers25012. Using Colors in Plots. ........................... 25112. 1. Setting the Colors of Objects25112.2. Mapping Variables to Colors25212.3. Using a Different Palette for a Discrete variable25412.4. Using a manually defined Palette for a Discrete variable25912.5. Using a Colorblind-Friendly Palette26112.6. USing a Manually defined Palette for a Continuous variable26312.7. Coloring a Shaded Region based on value26413. Miscellaneous Graphs26713. 1. Making a Correlation matrix26713.2. Plotting a Function27113.3. Shading a Subregion Under a Function Curve27213.4. Creating a Network Graph27413.5. Using Text Labels in a Network Graph27813.6. Creating a Heat Map28113. 7. Creating a Three-Dimensional Scatter Plot28313.8. Adding a Prediction Surface to a Three-Dimensional Plot28513.9. Saving a Three-Dimensional plot28913.10 Animating a Three-Dimensional Plot29113.11. Creating a Dendrogram29113.12. Creating a Vector Field29413.13. Creating a QQ Plot29913. 14. Creating a Graph of an Empirical Cumulative Distribution Function 30113. 15. Creating a Mosaic Plot30213.16. Creating a Pie Chart30713.17. Creating a Map30913. 18. Creating a Choropleth Map31313.19. Making a Map with a Clean Background317ⅵi| Table of contents13.20. Creating a Map from a Shapefile31914. Output for Presentation32314. 1. Outputting to PDF Vector Files32314.2. Outputting to SVG Vector Files32514.3. Outputting to WMF Vector Files32514.4. Editing a vector Output File32614.5. Outputting to Bitmap(PNG/TIFF) Files32714.6. Using Fonts in PDF Files33014.7. USing Fonts in Windows Bitmap or Screen Output33215. Getting Your Data into Shape.鲁鲁33515. 1. Creating a data frame33615.2. Getting Information About a Data Structure33715.3. Adding a Column to a Data frame33815.4. Deleting a Column from a Data frame33815.5. Renaming Columns in a Data frame33915.6. Reordering Columns in a Data Frame34015.7. Getting a Subset of a data frame34115.8. Changing the Order of Factor Levels34315.9. Changing the Order of Factor Levels Based on Data values34415.10. Changing the Names of Factor Levels34515.11 Removing Unused Levels from a Factor34715.12. Changing the Names of Items in a Character Vector34815.13. Recoding a Categorical Variable to Another Categorical Variable34915.14. Recoding a Continuous Variable to a Categorical Variable35115.15. Transforming Variables35215.16. Transforming Variables by group35415.17 Summarizing Data by groups35715.18 Summarizing data with Standard errors and Confidence intervals36115.19. Converting Data from Wide to Long36515.20. Converting Data from Long to wid.36815.21 Converting a Time Series object to Times and values369A. Introduction to ggplot2.Index,385Table of Contents