R Data Visualization Recipes - Vitor Bianchi Lanzetta - E-Book

R Data Visualization Recipes E-Book

Vitor Bianchi Lanzetta

0,0
27,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Translate your data into info-graphics using popular packages in R

About This Book

  • Use R's popular packages—such as ggplot2, ggvis, ggforce, and more—to create custom, interactive visualization solutions.
  • Create, design, and build interactive dashboards using Shiny
  • A highly practical guide to help you get to grips with the basics of data visualization techniques, and how you can implement them using R

Who This Book Is For

If you are looking to create custom data visualization solutions using the R programming language and are stuck somewhere in the process, this book will come to your rescue. Prior exposure to packages such as ggplot2 would be useful but not necessary. However, some R programming knowledge is required.

What You Will Learn

  • Get to know various data visualization libraries available in R to represent data
  • Generate elegant codes to craft graphics using ggplot2, ggvis and plotly
  • Add elements, text, animation, and colors to your plot to make sense of data
  • Deepen your knowledge by adding bar-charts, scatterplots, and time series plots using ggplot2
  • Build interactive dashboards using Shiny.
  • Color specific map regions based on the values of a variable in your data frame
  • Create high-quality journal-publishable scatterplots
  • Create and design various three-dimensional and multivariate plots

In Detail

R is an open source language for data analysis and graphics that allows users to load various packages for effective and better data interpretation. Its popularity has soared in recent years because of its powerful capabilities when it comes to turning different kinds of data into intuitive visualization solutions.

This book is an update to our earlier R data visualization cookbook with 100 percent fresh content and covering all the cutting edge R data visualization tools. This book is packed with practical recipes, designed to provide you with all the guidance needed to get to grips with data visualization using R. It starts off with the basics of ggplot2, ggvis, and plotly visualization packages, along with an introduction to creating maps and customizing them, before progressively taking you through various ggplot2 extensions, such as ggforce, ggrepel, and gganimate. Using real-world datasets, you will analyze and visualize your data as histograms, bar graphs, and scatterplots, and customize your plots with various themes and coloring options. The book also covers advanced visualization aspects such as creating interactive dashboards using Shiny

By the end of the book, you will be equipped with key techniques to create impressive data visualizations with professional efficiency and precision.

Style and approach

This book is packed with practical recipes, designed to provide you with all the guidance needed to get to grips with data visualization with R. You will learn to leverage the power of R and ggplot2 to create highly customizable data visualizations of varying complexities. The readers will then learn how to create, design, and build interactive dashboards using Shiny.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 333

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



R Data Visualization Recipes

 

 

 

 

 

 

 

 

 

 

A cookbook with 65+ data visualization recipes for smarter decision-making

 

 

 

 

 

 

 

 

 

 

Vitor Bianchi Lanzetta

 

 

 

 

BIRMINGHAM - MUMBAI

R Data Visualization Recipes

Copyright © 2017 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: November 2017

 

Production reference: 1201117

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-78839-831-2

 

www.packtpub.com

Credits

Author

Vitor Bianchi Lanzetta

Copy Editor

Tasneem Fatehi

Reviewer

Radovan Kavicky

Project Coordinator

Manthan Patel

Commissioning Editor

Amey Varangaonkar

Proofreader

Safis Editing

Acquisition Editor

Viraj Madhav

Indexer

Tejal Daruwale Soni

Content Development Editor

Tejas Limkar

Graphics

Tania Dutta

Technical Editor

Danish Shaikh

Production Coordinator

Shantanu Zagade

About the Author

Vitor Bianchi Lanzetta is a young economist and data science enthusiast. As soon as his graduation began at University of São Paulo, one of Latin America's most renowned universities, he was introduced to R by his statistics professor. it was love at first sight.

Since then, he has never stopped using it. He joined the university team to compete in the CFA Global Challenge, where he and his team had a whole valuation model translated into R along with a Monte Carlo simulation and a bunch of visualizations done through the language.

While researching commodity prices prediction, Vitor used R from adjusting simple conditional mean-based models to artificial neural networks, along with all the visualizations.

First, I would like to thank my family for all the support. Mom, dad, sister, love you beyond measure! I thank Amanda Carolina Teixeira, Ricardo Farias, and their families for being so nice to me. I wish you the best of success, happiness and love.I also would like to thank Professor Adriano Azevedo Filho for introducing me to the language and Packt for trusting me this project. Special thanks to Tejas Limkar for all the hard work.

About the Reviewer

Radovan Kavický is the principal data scientist and president at GapData Institute based in Bratislava, Slovakia, where he harnesses the power of data and wisdom of economics for public good.

He is a macroeconomist by education and consultant and analyst by professional background, with 8+ years of experience in consulting for clients from public and private sectors. He has strong mathematical and analytical skills and is able to deliver top-level research and analytical work.

From MATLAB, SAS & Stata switched to Python, R & Tableau.

Radovan is a member of the Slovak Economic Association (SEA) and evangelist of open data, open budget initiative and open government partnership. He is founder of PyData Bratislava, R <- Slovakia, and SK/CZ Tableau User Group (skczTUG).

He is a speaker at @TechSummit (Bratislava, 2017) and @PyData (Berlin, 2017), and member of the global Tableau #DataLeader network (2017).

You can follow him on Twitter at @radovankavicky, @GapDataInst, or @PyDataBA. His full profile and experience are available at https://www.linkedin.com/in/radovankavicky/ and https://github.com/radovankavicky.

GapData Institute: https://www.gapdata.org

<p>Practical Data Science,Data Visualization in Python, R & Tableau;Open Data and Public Administration in Slovakia

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

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 [email protected] for more details.

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.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788398319.

If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Table of Contents

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

Errata

Piracy

Questions

Installation and Introduction

Introduction

Installing and loading graphics packages

How to do it...

How it works...

There's more

See also...

Using ggplot2, plotly, and ggvis

Getting ready

How to do it...

How it works...

There's more

See also

Making plots using primitives

How to do it...

How it works...

There's more...

Plotting Two Continuous Variables

Introduction

Plotting a basic scatterplot

How to do it...

How it works...

There's more...

Hacking ggvis add_axis() function to operate as a title function

Getting ready

How to do it...

How it works...

Plotting a scatterplot with shapes and colors

How to do it...

How it works...

Plotting a shape reference palette for ggplot2

How to do it...

How it works...

There's more...

Dealing with over-plotting, reducing points

How to do it...

How it works...

There's more...

Dealing with over-plotting, jittering points

How to do it...

How it works...

Dealing with over-plotting, alpha blending

How to do it...

How it works...

There's more...

Rug the margins using geom_rug()

How to do it...

How it works...

Adding marginal histograms using ggExtra

Getting ready

How to do it...

How it works...

Drawing marginal histogram using gridExtra

Getting ready

How to do it...

How it works...

Crafting marginal plots with plotly

How to do it...

How it works...

Adding regression lines

How to do it...

How it works...

Adding quantile regression lines

Getting ready

How to do it...

How it works...

Drawing publish-quality scatterplots

Getting ready

How to do it...

How it works...

See also

Plotting a Discrete Predictor and a Continuous Response

Introduction

Installing car package and getting familiar to data

How to do it...

How does it works...

There is more

See also

Drawing simple box plots

Getting ready

How to do it...

How it works...

Adding notches and jitters to box plots

Getting ready

How to do it...

How it works...

Drawing bivariate dot plots using ggplot2

Getting ready

How to do it...

How does it work...

There is more

Using more suitable colors for geom_dotplot

Getting ready

How to do it...

How it works...

Combining box with dot plots

Getting ready

How it works...

How it works...

See also

Using point geometry to work as dots using ggvis, plotly and ggplot2

Getting ready...

How it works...

How it works...

There is more

Crafting simple violin plots

How to do it...

How it works...

Using stat_summary to customize violin plots

Getting ready

How it works...

How it works...

There is more...

Manually sorting and coloring violins 

Getting ready

How to do it...

How it works...

Using joy package to replace violins

Getting ready

How to do it...

How it works...

See also

Creating publication quality violin plots

Getting Ready

How to do it...

How it works...

 See also

Plotting One Variable

Introduction

Creating a simple histogram using geom_histogram()

Getting ready

How to do it...

How does it work...

Creating an histogram with custom colors and bins width

Getting ready

How to do it...

How it works...

Crafting and coloring area plots using geom_area() and more

Getting ready

How to do it...

How it works...

Drawing density plots using geom_density()

How to do it...

How it works...

See also

Drawing univariate colored dot plots with geom_dotplot()

Getting ready

How to do it...

How it works...

See also

Crafting univariate bar charts

Getting ready

How to do it...

How it works...

Using rtweet and ggplot2 to plot twitter words frequencies

Getting ready...

How to do it...

How it works...

See also

Drawing publish quality density plot

How to do it...

How it works...

Making Other Bivariate Plots

Introduction

Creating simple stacked bar graphs

Getting ready

How to do it...

How it works...

Crafting proportional stacked bar

Getting ready

How to do it...

How it works...

See also

Plotting side-by-side bar graph

Getting ready

How to do it...

How it works...

See also

Plotting a bar graphic with aggregated data using geom_col()

Getting ready

How to do it...

How it works...

Adding variability estimates to plots with geom_errrorbar()

Getting ready

How to do it...

How it works...

There's more...

See also

Making line plots

Getting ready

How to do it...

How it works...

Making static and interactive hexagon plots

Getting ready

How to do it...

How it works...

There is more...

Adjusting your hexagon plot

Getting ready

How to do it...

How it works...

See also

Developing a publish quality proportional stacked bar graph

Getting ready

How to do it...

How it works...

Creating Maps

Introduction

Making simple maps - 1854 London Streets

Getting ready

How to do it...

How it works...

Creating an interactive cholera map using plotly

Getting ready

How to do it...

How it works...

Crafting choropleth maps using ggplot2

Getting ready

How to do it...

How it works...

Zooming in on the map

Getting ready

How to do it...

How it works...

See also

Creating different maps based on different map projection types

Getting ready

How to do it... 

How it works...

See also

Handling shapefiles to map Afghanistan health facilities

Getting ready

How to do it...

How it works...

See also

Crafting an interactive globe using plotly

Getting ready

How to do it...

How it works...

Creating high quality maps

Getting ready

How to do it...

How it works...

See also

Faceting

Introduction

Creating a faceted bar graph

How to do it...

How it works...

Crafting faceted histograms

Getting ready

How to do it...

How it works...

Creating a facet box plot

How to do it...

How it works...

Crafting a faceted line plot

Getting ready

How to do it...

How it works...

There is more

Making faceted scatterplots

Getting ready

How to do it...

How it works...

Creating faceted maps

Getting ready

How to do it...

How it works...

See also

Drawing facets using plotly

How to do it...

How it works...

See also

Plotting a high quality faceted bar graph

How to do it...

How it works...

Designing Three-Dimensional Plots

Introduction

Drawing a simple contour plot using ggplot2

How to do it...

How it works...

Picking a custom number of contour lines

How to do it...

How it works...

Using the directlabels package to label the contours

Getting ready

How to do it...

How it works...

See also

Crafting a simple tile plot with ggplot2

How to do it...

How it works...

Creating simple raster plots with ggplot2

How to do it...

How it works...

There is more

Designing a three-dimensional plot with plotly

Getting ready

How to do it...

How it works...

Crafting a publication quality contour plot

How to do it...

How it works...

Using Theming Packages

Introduction

Drawing a bubble plot

Getting ready

How to do it...

How it works...

Popular themes with ggthemes

Getting Ready

How to do it...

How it works...

There's more...

See also

Applying sci themes with ggsci

Getting Ready

How to do it...

How it works...

See also

Importing new fonts with the extrafont package

Getting Ready

How to do it...

How it works...

There's more...

Using ggtech to mimic tech companies themes

Getting Ready

How to do it...

How it works...

There's more...

See also

Wrapping a custom theme function

Getting ready

How to do it...

How it works...

See also

Applying awesome themes and checking misspells with hrbrthemes

Getting Ready

How to do it...

How it works...

There's more...

See also

Designing More Specialized Plots

Introduction

Drawing wonderful facets zoom with the ggforce package

Getting Ready

How to do it...

How it works...

See also

Drawing sina plots with ggforce

Getting Ready

How to do it...

How it works...

Using ggrepel to plot non-overlaying texts

Getting Ready

How to do it...

How it works...

There's more...

See also

Visualizing relational data structures with ggraph

Getting Ready

How to do it...

How it works...

See also

Draw alternative lollipop and density plots with ggalt

Getting Ready

How to do it...

How it works...

See also

Making Interactive Plots

Introduction

Using ggiraph to create interactive plots

Getting ready

How to do it...

How it works...

See also

Using gganimate to craft animated ggplots

Getting ready

How to do it...

How it works...

See also

Crafting animated plots with tweenr

Getting ready

How to do it...

How it works...

See also

Building Shiny Dashboards

Introduction

Installing and loading a shiny package

How to do it...

How it works...

Creating basic shiny interactive plots

Getting ready

How to do it...

How it works...

See also

Developing intermediate shiny interactive plots

Getting ready

How to do it...

How it works...

There's more...

Building a shiny dashboard

Getting ready

How to do it...

How it works...

See also

Preface

Visualizations are amazing to investigate. You can explore data, reach insights, and transmit them directly to your audience. When several paragraphs may be needed to prove a point, good figures do it better in just a glance; they literally do. Fortunately, as computers keep evolving, crafting first-class graphs is not that difficult anymore.

R has proven itself an amazing tool for both data analysis and visualization. It all began by as an open source, well-designed, and multi-propose programming language that did not require much programming background to use. So, as R rose in popularity, a vivid and helpful community has also arisen.

The community itself has done much to keep improving the language. They have designed and shared tons of packages, which brings us to here and now. This book is full of examples on how to easily brew pretty graphics using very popular packages. It aims for teaching the nuts and bolts related to many different visuals, along with very useful tricks to adapt and enhance your figures.

Examples range from static scatterplots built using ggplot2 to interactive globes formed by plotly. Learn how to plot the most tweeted words by some user, draw true 3D interactive surfaces, and build shiny dashboards. Also you will learn how to apply a theme that will support your analysis.

What this book covers

Chapter 1, Installation and Introduction, teaches how to install the three most popular R visualization packages: ggplot2, ggvis, and plotly. This chapter introduces the ways a package can be installed: either from a CRAN repository or a GitHub one. You also get to know how to call functions from those packages while going through some examples demonstrating the basic framework of those packages.

Chapter 2, Plotting Two Continuous Variables, dives into scatterplots, the most popular way to plot two continuous variables. We learn how to change shapes, colors and sizes. The main problem imbued to scatterplots is called over plotting; this chapter explores related solutions. Also we see how to draw marginal plots and produce high-quality scatterplots.

Chapter 3, Plotting a Discrete Predictor and a Continuous Response, covers distributions over categories, a great way to explore your data. It can be handled using R to draw one box plots, bivariate dot plots, and violin plots. Learn how to draw and combine each of them. Meet nice additional features while learning the art of crafting to publish quality violin plots.

Chapter 4, Plotting One Variable, covers histograms, density plots, area plots, univariate dot plots, and univariate bar graphs. Learn how to brew these visuals under different R packages. Meet the useful modifications achieved by little code tweaks.

Chapter 5, Making Other Bivariate Plots. Trust this chapter to build bars of all kinds, line graphs, hexagon plots, and deploy variability estimates (error bars). Bars can be displayed as stacked, proportional, and/or side by side. Despite the wide variability of bars available, there are also some useful tricks to learn.

Chapter 6, Creating Maps, Demonstrating spatial correlation is mustily done through maps. Learn how points, lines, and polygons can be used to craft maps through R packages. Meet the John Snow cholera map, Tolkien's Middle Earth map, and an interactive globe of banking crisis. Also learn how to read shapefiles from temporary directories and how projection types can be coerced.

Chapter 7, Faceting, covers an important device that allows comprehension on more complex data relations. Recipes within this chapter teach how different kinds of visuals can easily reach out for faceting using ggplot2. Additionally, this chapter teaches how those can be coerced into interactive visuals by relying on the plotly package.

Chapter 8, Designing Three-Dimensional Plots. Although ggplot2 is not capable of drawing true 3D surfaces, it can draw their 2D representations. This chapter demonstrates the craft of contour, tile, and raster plots using ggplot2. It also demonstrates how to draw true and interactive 3D surfaces using plotly.

Chapter 9, Using Theming Packages, teaches how several themming packages can be used to coerce several themes to a ggplot. These range from themes inspired by The Simpsons and Star Trek to respectful academic journal themes. Also learn how to wrap a theme of your own into a function.

Chapter 10, Designing More Specialized Plots. Meet the complementary ggplot2 packages: ggforce, ggrepel, ggraph, and ggalt. These can be used to simple tasks like zooming in to more complex tasks like avoiding labels and texts inside the graph to overlay each other.

Chapter 11, Making Interactive Plots, explains how the ggiraph, gganimate, and tweenr packages can be used to create ggplot2 based animations and design interactive features. Animations are an useful way to denote how data variables have evolved with wonderful applications to all kinds of visuals.

Chapter 12, Building Shiny Dashboards. shiny allows the user to make interactive web applications direct from R. The package shinydashboard makes easier to create dash boards using shiny. This recipe demonstrates how to construct those while customizing styles, side bars, and more.

What you need for this book

First things first, download the latest version of R (https://cran.r-project.org/). Additionally, recipes were written using a IDE named RStudio (free version). I do prefer to write R codes under RStudio IDE; if you do not have the software yet, you can get it from https://www.rstudio.com/products/rstudio/download/.

Who this book is for

If you are looking to create custom data visualization solutions using the R programming language and are stuck somewhere in the process, this book will come to your rescue. Prior exposure to packages such as ggplot2 will be useful but not necessary. Some programming knowledge of R is required for this book.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "This chapter covers basic aspects of three of them:ggplot2,plotly, and ggvis."

A block of code is set as follows:

> install.packages(c('devtools','plotly','ggvis'))> devtools::install_github('hadley/ggplot2')

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Open your RStudio, go to Tools|Install Packages..."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:

Log in or register to our website using your email address and password.

Hover the mouse pointer on the

SUPPORT

tab at the top.

Click on

Code Downloads & Errata

.

 

Enter the name of the book in the

Search

box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on

Code Download

.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/R-Data-Visualization-Recipes. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Installation and Introduction

Following recipes are covered in this chapter:

Installing and loading graphics packages

Using ggplot2, plotly, and ggvis

Making plots using primitives

Introduction

R is a free open language and environment for statistical computing and graphics. It particularly gained wide popularity among scientists from different fields, journalists, and private companies. There are various reasons for that, openness and gratuity may be couple of them. Also, R requires minimal programming background and has a vibrant online community.

From community, a bunch of useful graphical packages had come. This chapter covers basic aspects of three of them: ggplot2, plotly, and ggvis. The first one (ggplot2) has been there for a long time, is very mature, and is very useful to build non-interactive graphics.

Both plotly and ggvis are much younger packages, which can build interactive plots. Both are shiny compatible and can well address the matter of web applications. Beginning with installation and loading, this chapter goes all the way through explaining the basic framework of all those three packages, while demonstrating how to use ggplot2 primitives.

Installing and loading graphics packages

Before starting, there are some habits you may want to cultivate in order to keep improving your R skills. First of all, whenever you program there may be some challenges to face. Usually those are tackled either by out-thinking the problem or by doing some research. You might want to remember what the problem was about and the solution, be that for times you face it again later or even for studying hours, keep a record of problems and solutions.

Speaking for me, making a library-like folder and gathering some commented examples on problems and resolutions was, and still is, of great help. Naming files properly and taking good use of comments (# are used to assign comments with R) makes the revision much easier.

R Markdown documents are pretty useful if want to keep a track of your own development and optionally publish for others to see. Publishing the learning process is a good way to self-promote. Also, keep in mind that R is a programming language and often those can correctly pull a problem out in more than one way, be open-minded to seek different solutions.

First things first, in order to make good use of a package, you need to install the package and know how to call a package's function.

If your R Session is running for a long time, there is a good chance that a bunch of packages are already loaded. Before installing or updating a package it's a good practice to restart R so that the installation won't mess with related loaded packages.

How to do it...

Run the following code to install the graphics packages properly:

> install.packages(c('devtools','plotly','ggvis'))> devtools::install_github('hadley/ggplot2')

How it works...

Most of the book covers three graphic packages—ggplot2, plotly, and ggvis. In order to install a new package, you can type the function install.packages() into the console. That function works for packages available at CRAN-like repositories and local files. In order to install packages from local files, you need to name more than just the first argument. Entering ?install.packages into RStudio console shall lead you to the function documentation at the Help tab.

Instants after running the recipe, all the packages (devtools included) covered in this chapter might already be properly installed. Check the Packages tab in your RStudio application (speed up the search by typing into the search engine); if everything went fine, these four may be shown under UserLibrary. Following image shows how it might look like:

Figure 1.1 - RStudio package window (bottom right corner).

If it fails, you may want to check the spelling and the internet connection. This function also gives some outputs that stand for warnings, progress reports, and results. Look for a message similar to  package '<Package Name>' successfully unpacked and MD5 sums checked to make sure that all went fine. Checking the output is a good practice in order to know if the plan worked. It also give good clues about troubleshooting.

You may want to call a non-existing package (be creative here) and a package already installed and see what happens. Sometimes incompatibilities avoid proper download and installation. For example, missing Java or the proper architecture of Java may prevent you from installing the rJava package.

Realize that a package's name must be in the string format in order to work (remember to use ' '). It's also important to check the spelling. The function (calling and arguments) is case sensitive; if you miss even one letter or case, you will not find the desired package. Also note that the arguments where drew into a c() function. That is a vector (try ?c in the console).

? sign is actually a function that comes along base package called utils. Typing ?<function name> will always lead you to documentation whenever there is one to display. All functions coming from CRAN packages, base R and maybe the majority of GitHub ones have related documentation files, yet, if it's not base R do not forget to have the respective package already loaded. Alternatively you can also make calls like this: ?<package name>::<function name>.

As first argument of the install.packages() function, a vector of strings was given. That said, multiple packages can be downloaded and installed simultaneously. The same function might not install only the packages asked, but all the packages each of them rely on.

Once the packages are installed, you have a bunch of new functions at your disposal. In order to get to know these functions, you can seek the packages' documentation online. Usually, the documentations can be found at repositories (CRAN, GitHub, and so on).

Now with a bunch of new functions at hand, the next step is to call a function from a specific package. There are several ways of doing that. One possible way to do it is typing <package name>::<package function>, latest code block done that when called install_github(), a function from coming from devtools package, so it was called this way: devtools::install_github().

There are pros and cons about calling a function this way. As for pros, you mostly avoid any name conflict that could possible happen between packages. Other than that, you also avoid loading the whole package when you only need to call a single function. Thus, calling a function this way may be useful in two occasions:

Name conflict is expected

Only few functions from that package may be requested and only a few times

Otherwise, if a package is required many times, typing <package name>:: before every function is anti-productive. It's possible to load and attach the whole package at once. Via RStudio interface, right below the window that shows environment objects, there is a window with a package tab. Below the package tab it's possible to check the box in order to load a package and uncheck to detach them.

Try to detach ggplot2 by unchecking the box; keep an eye on that box. You can load packages using functions. The require() and library() functions can be assigned to this task. Both don't need ' ' in order to function well like install.packages() does, but if you call the package name as a string it stills works. Note that both functions can only load one package a time.

Although require() and library() work in a very similar way, they do not work exactly the same. If require() fails it throws a warning, library() on the other hand will trow an error. There is more, require() returns a logical value that stands for TRUE when the load succeeds and FALSE when it fails; library() returns no value.

For common loading procedures that is not a difference that should made into account, but if you want to create a function or loop that depends on loading a package and checking if it succeed, you may find easier to make it using require(). Using the logical operator & (and), it's possible to load all three packages at once and store the result in a single variable. Calling this variable will state TRUE if there is success for all and FALSE if a single one fails. This is done as follows:

> lcheck <- require(ggplot2) & require(plotly) & require(ggvis)> lcheck

lcheck won't tell you which and how many packages failed. Try assigning c( require(ggplot2), require(plotly), reqruire(ggvis)) instead. Each element returning a FALSE is the package that is giving you trouble; this means better chances at troubleshooting.

For now you might be able to install R packages - from CRAN, Git repositories or local files - load and call a functions from an specific package. Now that you are familiar with R package's installation and loading procedures, the next section gives an introduction to the ggplot2 package framework.

There's more

Installation is also possible via RStudio features, which may seen more user friendly for newcomers. Open your RStudio, go to Tools > Install Packages..., type the packages' names (separate them with space or comma), and hit install. It fills the install.package() function and shows it in your console.

This is most indicated when you are not absolutely sure about the package name, but have a good clue. There is automatic suggestion thing that shall help you out to figure exactly what the package name is. You can also install packages from local files by using this feature. Look for an option called Install from and switch it to Package Archive File instead of Repository.

RStudios also gives you a Check For Packages Updates... option right below Install Packages... Hit it once in a while to make sure your packages are properly updated. Along with the packages to be updated it also shows what is new about them.

See also...

ggplot2

tidyverse reference manual: 

http://ggplot2.tidyverse.org/reference/

ggvis

CRAN-R documentaion: 

https://cran.r-project.org/web/packages/ggvis/ggvis.pdf

plotly

figure reference: 

https://plot.ly/r/reference/

Using ggplot2, plotly, and ggvis

ggplot2, ggvis, and plotly have proven to be very useful graphical packages in the R universe. Each of them gained a respectful sum of popularity among R users, being recalled for the several graphical tasks each of them can handle in very elegant manners.

The purpose of this section is to give a brief introduction on the general framework of ggplot2 via some basic examples, and relate how to tackle similar quests using ggvis and plotly. Along the way, some pros and cons from each package will be highlighted.

Whenever you need to choose between some packages (and base R), it's important to balance the tasks each one were designed to handle, the amount of work it will require for you to achieve your goal (learning time included), and the time you actually have. It's also good to consider scale gains in future uses. For example, mastering ggplot2 may not seem a smart choice for a single time task but might pay-off if you're expecting lots of graphical challenges in the future.

Keep in mind that all the three packages are eligible for a large convoy of tasks. There are some jobs that a specific package is more suitable for and even some tasks that can be considered almost impracticable for others. This point will become clearer as the book goes on.

Getting ready

The only requirement this section holds is to have the ggplot2, ggvis, and plotly packages properly installed. Go back to Installing and loading graphics packages recipe if that is not the case. Once the installation is checked, it's time to know ggplot2 framework.