Book Image

R Data Visualization Recipes

By : Vitor Bianchi Lanzetta
Book Image

R Data Visualization Recipes

By: Vitor Bianchi Lanzetta

Overview of this book

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.
Table of Contents (19 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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 ggiraphgganimate, 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..."

Note

Warnings or important notes appear like this.

Note

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:

  1. Log in or register to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.

 

  1. Enter the name of the book in the Search box.
  2. Select the book for which you're looking to download the code files.
  3. Choose from the drop-down menu where you purchased this book from.
  4. 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.