R Graph Essentials
()
About this ebook
R has undergone an explosion of popularity around the world. The wide range of graphs and options for creating high-impact visualizations makes R a highly effective tool for researchers, data analysts, students, and others.
This book starts with the basics of R and the principles of creating graphics, focusing on scatterplots and line graphs for research and data analysis.
After that you will learn how to include mathematical expressions on your graphs and create bar charts, histograms, boxplots, and so on using advanced functions. The book then dives deeper into shading and coloring your graphs and labeling points on graphs using the qplot function. After that you will learn how to use the ggplot package to control symbol color, size, shape, and many other attributes.
By the end of this book, you will have expert skills to create attractive and informative graphics and design professional-level graphs.
Related to R Graph Essentials
Related ebooks
RStudio for R Statistical Computing Cookbook Rating: 0 out of 5 stars0 ratingsR High Performance Programming Rating: 4 out of 5 stars4/5Learning RStudio for R Statistical Computing Rating: 4 out of 5 stars4/5Hands-On Time Series Analysis with R: Perform time series analysis and forecasting using R Rating: 0 out of 5 stars0 ratingsIntroduction to R for Business Intelligence Rating: 0 out of 5 stars0 ratingsR Machine Learning Essentials Rating: 0 out of 5 stars0 ratingsMastering Data Analysis with R Rating: 5 out of 5 stars5/5R for Data Science Rating: 5 out of 5 stars5/5Learning Shiny Rating: 0 out of 5 stars0 ratingsR Object-oriented Programming Rating: 3 out of 5 stars3/5Simulation for Data Science with R Rating: 0 out of 5 stars0 ratingsPractical Data Analysis - Second Edition Rating: 0 out of 5 stars0 ratingsWeb Application Development with R Using Shiny - Second Edition Rating: 0 out of 5 stars0 ratingsLearn R Programming in 24 Hours Rating: 0 out of 5 stars0 ratingsLearning Bayesian Models with R Rating: 5 out of 5 stars5/5Mastering Predictive Analytics with R Rating: 4 out of 5 stars4/5R Machine Learning By Example Rating: 0 out of 5 stars0 ratingsMastering Text Mining with R Rating: 0 out of 5 stars0 ratingsJust Enough R: Learn Data Analysis with R in a Day Rating: 4 out of 5 stars4/5Instant Heat Maps in R How-to Rating: 0 out of 5 stars0 ratingsMastering Machine Learning with R - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Python for Data Science Rating: 3 out of 5 stars3/5Learning Probabilistic Graphical Models in R Rating: 0 out of 5 stars0 ratingsPython Data Science Essentials Rating: 0 out of 5 stars0 ratingsLearning Tableau Rating: 0 out of 5 stars0 ratingsPractical Data Science Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Machine Learning with R Rating: 0 out of 5 stars0 ratingsGetting Started with Python Data Analysis Rating: 0 out of 5 stars0 ratingsIntroduction to Data Science Using R Rating: 0 out of 5 stars0 ratingsLearning Tableau 10 - Second Edition Rating: 4 out of 5 stars4/5
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5TensorFlow in 1 Day: Make your own Neural Network Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5C# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsUnity from Zero to Proficiency (Beginner): Unity from Zero to Proficiency, #2 Rating: 5 out of 5 stars5/5C++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratings
Reviews for R Graph Essentials
0 ratings0 reviews
Book preview
R Graph Essentials - David Alexander Lillis
Table of Contents
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Base Graphics in R – One Step at a Time
Learning basic graphics techniques
Creating and joining points
Creating scatterplots and line plots
Colors in R
Passing parameter values to titles and labels
Including a regression line
Graphing mathematical functions
R provides many options
Creating graphs with several curves
Customizing your axes
Creating axis labels
Creating multiple graphs on the same page
Saving your graphs
Including mathematical expressions on your plots
Summary
2. Advanced Functions in Base Graphics
Reading datasets into R
Including a regression line and residuals
A medical dataset to create graphs
Creating complex multiple axes
Superposing graphs
Creating point labels
Including a grid on your graph
Shading and coloring your graph
Using polygon() to shade under a normal curve
Creating bar charts
Including a legend
Creating histograms
Creating boxplots
Creating pie charts
Creating dotcharts
R's color palettes
Using smoothers on your graph
Creating scatterplot matrices
Writing functions to create graphs
Including error bars on your graph
Summary
3. Mastering the qplot Function
About qplot
The qplot syntax
Producing scatterplots using qplot
Subsetting your data before graphing
Mapping aesthetics to categorical variables
Controlling colors on your graph
Setting up graphs as objects
Creating facet plots
Creating line graphs using qplot
Creating multiple curves simultaneously
Including smoothed curves
Creating histograms with qplot
Creating facet plots for histograms
Creating kernel density plots
Creating bar charts
Creating boxplots
Creating graphs with dates
Summary
4. Creating Graphs with ggplot
Getting started with ggplot
Mapping color, shape, and size to a variable
Modifying the plotting background
Controlling the legend name and legend labels
Modifying the x and y axes
Creating attractive color schemes
Creating curves for each factor level
Creating histograms
Creating bar charts
Creating a stacked bar chart
Creating a grouped bar chart
Creating a faceted bar chart
Creating boxplots
Labeling points with text
Mapping color to text
Including regression lines
Summary
Index
R Graph Essentials
R Graph Essentials
Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2014
Production reference: 1150914
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78355-455-3
www.packtpub.com
Cover image by Arvind Shetty (<arvindshetty86@gmail.com>)
Credits
Author
David Alexander Lillis
Reviewers
Mzabalazo Z. Ngwenya
Donato Teutonico
Tengfei Yin
Commissioning Editor
Pramila Balan
Acquisition Editor
Reshma Raman
Content Development Editor
Akashdeep Kundu
Technical Editor
Rohit Kumar Singh
Copy Editors
Roshni Banerjee
Adithi Shetty
Project Coordinator
Kartik Vedam
Proofreaders
Simran Bhogal
Maria Gould
Ameesha Green
Indexer
Hemangini Bari
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
About the Author
David Alexander Lillis is an experienced researcher and statistician, having worked in research evaluation, agricultural and forestry statistics, and education research and statistics. Currently, David delivers lectures in mathematics, statistics, and research methods at the New Zealand Institute of Sport in New Zealand. He is the owner and Director of Sigma Statistics and Research Limited, a New Zealand-based consultancy specializing in training, software development, data analysis, and statistical modelling using R. David holds an Honor's degree and Master's degree in Physics and Mathematics, and a PhD from Curtin University in Perth, Australia.
I wish to thank my family—Anna (my wife), David (my eight-year old son), and Coral (my mother) for their patience and support while I worked on this book. In addition, I wish to thank the three reviewers for their helpful feedback.
About the Reviewers
Mzabalazo Z. Ngwenya has worked extensively in the field of statistical consulting and currently works as a biometrician. He holds an MSc degree in Mathematical Statistics from the University of Cape Town, and is currently pursuing a PhD. His research interests include statistical computing, machine learning, and spatial statistics. Previously, he was involved in reviewing Learning RStudio for R Statistical Computing, Mark P.J. van der Loo and Edwin de Jonge, Packt Publishing; R Statistical Application Development by Example Beginner's Guide, Prabhanjan Narayanachar Tattar, Packt Publishing; and Machine Learning with R, Brett Lantz, Packt Publishing.
Donato Teutonico has several years of experience in the modeling and simulation of drug effects and clinical trials in industrial and academic settings. He received his Pharm.D. degree from the University of Turin, Italy, specializing in Chemical and Pharmaceutical Technology, and his PhD in Pharmaceutical Sciences from Paris-Sud University, France.
He is the author of two R packages for Pharmacometrics: CTS template and panels-for-pharmacometrics; both are available on Google code. He is also the author of Instant R Starter, Packt Publishing.
Tengfei Yin earned his BS degree in Biological Science and Biotechnology from Nankai University in China and a PhD in Molecular, Cellular and Developmental Biology (MCDB) with a focus on computational biology and bioinformatics from Iowa State University. His research interests include information visualization, high-throughput biological data analysis, data mining, machine learning, and applied statistical genetics. He has developed and maintained several software packages in R and Bioconductor.
www.PacktPub.com
Support files, eBooks, discount offers, and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Preface
Reading this book will enable you to learn very quickly how to create wonderful graphics using R. Since R is based on syntax, the time required to master R can be considerable. However, creating high quality and attractive graphics is made easy through the syntax and step-by-step explanations of this book. By reading this book, you will learn how to introduce attractive color schemes, create headings and legends, design your own axes and axes labels, create mathematical expressions on your graphs, and much more.
What this book covers
Chapter 1, Base Graphics in R – One Step at a Time, introduces the basic components of a graph (headings, symbols, lines, colors, axes, labels, legends, and so on) and outlines how to use the R syntax to create these components.
Chapter 2, Advanced Functions in Base Graphics, covers the techniques required to create professional-level graphs in R, including bar charts, histograms, boxplots, pie charts, and dotcharts. It also covers regression lines, smoothers, and error bars.
Chapter 3, Mastering the qplot Function, explains how to use qplot to create a wide range of basic but attractive graphs.
Chapter 4, Creating Graphs with ggplot, introduces you to ggplot,