Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
R Data Visualization Recipes
R Data Visualization Recipes

R Data Visualization Recipes: A cookbook with 65+ data visualization recipes for smarter decision-making

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

R Data Visualization Recipes

Plotting Two Continuous Variables

In this chapter, we will cover the following recipes:

  • Plotting a basic scatterplot
  • Hacking ggvis add_axis() function to operate as a title function
  • Plotting a scatterplot with shapes and colors
  • Plotting a shape reference palette for ggplot2
  • Dealing with over-plotting, reducing points
  • Dealing with over-plotting, jittering points
  • Dealing with over-plotting, alpha blending
  • Rug the margins using geom_rug()
  • Adding marginal histograms using ggExtra
  • Drawing marginal histogram using gridExtra
  • Crafting marginal plots with plotly
  • Adding regression lines
  • Adding quantile regression lines
  • Drawing publish-quality scatterplots

Introduction

Investigating the relationship between two variables may be much easier than investigating it for several variables simultaneously. There is one good reason for that: we can visualize bivariate relationships way better. Problems with numerous amount of variables are often split into several problems with only two variables.

There are several visualizations that supports the two variables context. The most popular of them may be the scatterplots. People are familiar with them, on the other hand there is a problem that haunts many scatterplots: over-plottingting. This chapter begins with recipes to draw very simple scatterplots, going all the way to explore solutions available when it comes to deal with over-plottingting, and demonstrate how enhance scattersplots by setting up marginal graphics.

Plotting a basic scatterplot

Scatterplots play a major role in the representation of two continuous variables. Making simple scatterplots is a very easy task to handle using ggplot2, ggvis, or plotly. This recipe uses a data frame called iris to draw plots, it comes with base R (datasets package).

Before using data coming from a package, you may want to try entering ?<package name>::<data frame name> into your console. For this recipe, that would go as: ?datasets::iris. This is may lead you towards data documentation, this way you get to know each variable coming from the data frame.

From the various features presented by this data set, this recipe uses Petal.Width and Petal.Length. They respectively account for iris' petal widths and lengths measured in centimeters. Besides drawing the plots, this recipe also teaches how to add a title to them...

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

Version 0.4.3 of ggvis does not have a function to add titles to plots, but still there is a known way to hack the add_axis() function to work as a title function. If a user expects to explore this device many times, it's advised to wrap it into a function. Besides making the code more readable, it's a quicker way to address the problem.

Getting ready

This recipe does not only teach how to craft the hack function but also to experiment it on the previous plot, so make sure to have sca3 from the earlier recipe loaded into your environment. Alternatively, you can use another ggvis object of your own.

...

Plotting a scatterplot with shapes and colors

There are several aesthetics coming out from geom_points() that can be changed. Typing ?geom_point into the R console will take you to the function documentation, which comes with a complete list of aesthetics understood by the function. The mandatory ones come in bold.

Names given are nothing but self-explanatory. Besides the mandatory x and y values, optional values range from alpha to stroke. For this particular recipe, we're settling for changes in the shape and colours arguments. Recipe  also aims for similar results using both ggvis and plotly

How to do it...

  1. Change the shape and colour arguments to get a better result:
> library...

Plotting a shape reference palette for ggplot2

Shapes are picked following a default scale when you input a variable to work as shape using ggplot2. You can always choose to tweak this scale to one of your preference. To do so you need to know which shapes are available and how you can call for them. This recipe simply draws the following shape palette:

Figure 2.4 - ggplot2 shape palette.

It shows available points, plus the number used to call for them. Now let's explore the code that built it.

How to do it...

Draw a suitable data frame using rep() and seq() functions, them let's plot those using geom_point():

> palette <- data.frame(x = rep(seq(1,5,1),5))
> palette$y <- c(rep(5,5),rep(4,5)...

Introduction


Investigating the relationship between two variables may be much easier than investigating it for several variables simultaneously. There is one good reason for that: we can visualize bivariate relationships way better. Problems with numerous amount of variables are often split into several problems with only two variables.

There are several visualizations that supports the two variables context. The most popular of them may be the scatterplots. People are familiar with them, on the other hand there is a problem that haunts many scatterplots: over-plottingting. This chapter begins with recipes to draw very simple scatterplots, going all the way to explore solutions available when it comes to deal with over-plottingting, and demonstrate how enhance scattersplots by setting up marginal graphics.

Plotting a basic scatterplot


Scatterplots play a major role in the representation of two continuous variables. Making simple scatterplots is a very easy task to handle using ggplot2, ggvis, or plotly. This recipe uses a data frame called iris to draw plots, it comes with base R (datasets package).

Note

Before using data coming from a package, you may want to try entering ?<package name>::<data frame name> into your console. For this recipe, that would go as: ?datasets::iris. This is may lead you towards data documentation, this way you get to know each variable coming from the data frame.

From the various features presented by this data set, this recipe uses Petal.Width and Petal.Length. They respectively account for iris' petal widths and lengths measured in centimeters. Besides drawing the plots, this recipe also teaches how to add a title to them. So, move on to the coding!

How to do it...

  1. Initialize a ggplot and then give it the point geometry:
> library(ggplot2)
> sca1 ...

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


Version 0.4.3 of ggvisdoes not have a function to add titles to plots, but still there is a known way to hack the add_axis() function to work as a title function. If a user expects to explore this device many times, it's advised to wrap it into a function. Besides making the code more readable, it's a quicker way to address the problem.

Getting ready

This recipe does not only teach how to craft the hack function but also to experiment it on the previous plot, so make sure to have sca3 from the earlier recipe loaded into your environment. Alternatively, you can use another ggvis object of your own.

How to do it...

  1. Wrap the add_axis() function with several arguments declared to work as a title function:
> library(ggvis)
> ggvis_title <- function(vis, plot_title, title_size = 18, shift = 0, ...){ 
    add_axis(vis, 'x', ticks = 0, orient = 'top', 
             properties = axis_props( axis = list(strokeWidth = 0),
       ...

Plotting a scatterplot with shapes and colors


There are several aesthetics coming out from geom_points() that can be changed. Typing ?geom_point into the R console will take you to the function documentation, which comes with a complete list of aesthetics understood by the function. The mandatory ones come in bold.

Names given are nothing but self-explanatory. Besides the mandatory x and y values, optional values range from alpha to stroke. For this particular recipe, we're settling for changes in the shape and colours arguments. Recipe  also aims for similar results using both ggvis and plotly

How to do it...

  1. Change the shape and colour arguments to get a better result:
> library(ggplot2)
> sca1 <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width))
> sca1 + geom_point(aes(shape = Species, colour = Species))

Now each iris species is designated by a unique combination of shapes and colors:

Figure 2.3 - Adding shapes and colors to a scatter plot.

  1. plotly can also handle such...

Plotting a shape reference palette for ggplot2


Shapes are picked following a default scale when you input a variable to work as shape using ggplot2You can always choose to tweak this scale to one of your preference. To do so you need to know which shapes are available and how you can call for them. This recipe simply draws the following shape palette:

Figure 2.4 - ggplot2 shape palette.

It shows available points, plus the number used to call for them. Now let's explore the code that built it.

How to do it...

Draw a suitable data frame using rep() and seq() functions, them let's plot those using geom_point():

> palette <- data.frame(x = rep(seq(1,5,1),5))
> palette$y <- c(rep(5,5),rep(4,5),rep(3,5),rep(2,5),rep(1,5))
> library(ggplot2)
> ggplot(data = palette,aes(x,y)) +
    geom_point(shape = seq(1,25,1), size = 10, fill ='white') +
    scale_size(range = c(2, 10)) +
    geom_text(nudge_y = .3, label = seq(1,25,1))

Function geom_text() is plotting the reference numbers related...

Dealing with over-plotting, reducing points


There are mainly three techniques used to deal with over-plot. They are: (i) adopting smaller points,(ii) jittering data, and (iii) alpha blending. These are useful tools, not only to deal with over-plot but also to check if there is over-plotting.

However, these are not the only options; for example, alternative geometries can also be implemented. No matter how troublesome over-plotting may be there are good solutions available.There is not a single solution that is better for all the situations, so you must know a bunch of them. 

This recipe advises how to apply a technique based on point size reduction using ggplot2, ggvis and plotly. In order to do so, we are trusting the ggplot2::diamonds data frame. Keep in mind that reducing points works better for cases where points are very close to each other but do not actually occupy the same coordinates.

How to do it...

  1. Set shape to '.' in order to reduce points using ggplot2:
> library(ggplot2)
&gt...

Dealing with over-plotting, jittering points


Size reduction is never an option when there are too many points sharing the exact same coordinates; it simply is not the right tool for the job. A clear option therefore is to jitter the data, that is, add a little noise to the data so that the points move around a little bit and the over-plotting kind of wears off.

Two points must be highlighted here. Jittering may be a good way to adjust the plot but not to adjust the data, so do not use jittered data for modeling and always be honest when transformations of that nature take place. Second point is that as long it may work pretty well when many points share coordinates. Although, if too many points are only close enough but do no share same coordinates there is a chance that jittering will work very badly.

Now let's go back to the iris data set and demonstrate how this technique can be applied using ggplot2, ggvis and plotly.

How to do it...

  1. With ggplot2, set potion = 'jitter' in order to obtain...

Dealing with over-plotting, alpha blending


Another popular technique is known as alpha blending. It consists on making points translucent, this way the audience gets to know if points are stacked or not.  Between all the techniques demonstrated so far, alpha blending must be the most popular one. This recipe teaches how to apply alpha blending using ggplot2, plotly, and ggvis.

How to do it...

  1. Set the alpha parameter to apply alpha blending to ggplot:
> library(ggplot2)
> sca1 <- ggplot( iris, aes( x = Petal.Length, y = Petal.Width))
> sca1 + geom_point( alpha = .5 , 
                     aes(shape = Species, colour = Species))

The following figure 2.7 shows alpha blending working:

Figure 2.7 - alpha blending with ggplot2.

  1. Setting alpha parameter with plotly will also apply alpha blending:
> library(plotly)
> sca9 <- plot_ly( iris, x = ~Petal.Length, y = ~Petal.Width, 
>                  type = 'scatter', mode = 'markers', alpha = .5, symbol = ~Species)
> sca9
  1. ggvis applies...

Rug the margins using geom_rug()


Up till now, the chapter has focused on how to draw scatterplots and solutions related to over-plotting. Upcoming recipes, including this one, shall focus on enhancing scatterplots. If there is a bivariate relation to be displayed there is also two univariate distributions to show. How can they be used to improve the plots? 

Answer lies in filling the margins with supplemental plots carrying representations of underlying univariate distributions. Still relying on the iris data set framework, this recipe introduces a simple solution, almost restricted to ggplot2. Let's rug plots in the margins with geom_rug().

How to do it...

  1. Draw a scatterplot using ggplot2 and sum the geom_rug() layer:
> set.seed(50) ; library(ggplot2)
> rug <- ggplot(iris,
                aes(x = Petal.Length, 
                    y = Petal.Width, 
                    colour = Species))
> rug <- rug +
    geom_jitter(aes( shape = Species), alpha = .4) +
    geom_rug(position...

Adding marginal histograms using ggExtra


Another way to go is to draw histograms or even density distributions in the margins. Drawing tailor made plots in the margins would require more code. On the other hand, if there is no need for greater customization ggExtra package can be used to spare many code lines. This recipe is demonstrating how to use ggExtra to easily draw histograms in the margins of a scatterplot. 

Getting ready

In order to properly execute this recipe, the ggExtra package must be locked and loaded. Run the following code to make that happen:

> if( !require(ggExtra)){ install.packages('ggExtra')}

Once ggExtra is installed we can go on.

How to do it...

  1. Draw a ggplot2 scatterplot like this:
library(ggplot2)
base_p <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species))
scatter <- base_p + geom_point( alpha = .5, aes(shape = Species)) + 
  geom_rug(alpha = .5, sides = 'tr', show.legend = F) +
  theme(legend.position = 'bottom')
  1. Load ggExtra and input the...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 22, 2017
Length: 366 pages
Edition : 1st
Language : English
ISBN-13 : 9781788394079
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 22, 2017
Length: 366 pages
Edition : 1st
Language : English
ISBN-13 : 9781788394079
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
R Data Analysis Projects
€41.99
R Data Mining
€36.99
R Data Visualization Recipes
€24.99
Total 103.97 Stars icon
Banner background image

Table of Contents

12 Chapters
Installation and Introduction Chevron down icon Chevron up icon
Plotting Two Continuous Variables Chevron down icon Chevron up icon
Plotting a Discrete Predictor and a Continuous Response Chevron down icon Chevron up icon
Plotting One Variable Chevron down icon Chevron up icon
Making Other Bivariate Plots Chevron down icon Chevron up icon
Creating Maps Chevron down icon Chevron up icon
Faceting Chevron down icon Chevron up icon
Designing Three-Dimensional Plots Chevron down icon Chevron up icon
Using Theming Packages Chevron down icon Chevron up icon
Designing More Specialized Plots Chevron down icon Chevron up icon
Making Interactive Plots Chevron down icon Chevron up icon
Building Shiny Dashboards Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(1 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Amazon Customer Jan 16, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A clear and compelling read, and a great way to get a handle on the major methods of data visualization in R.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.