Book Image

R Data Visualization Cookbook

By : Gohil
Book Image

R Data Visualization Cookbook

By: Gohil

Overview of this book

If you are a data journalist, academician, student or freelance designer who wants to learn about data visualization, this book is for you. Basic knowledge of R programming is expected.
Table of Contents (12 chapters)
11
Index

Creating a hexbin plot


Hexbin plots can be viewed as an alternative to scatter plots. The hexagon-shaped bins were introduced to plot densely packed sunflower plots. They can be used to plot scatter plots with high-density data. We will use the hexbin package available in R to plot hexagon-shaped bins on a plot instead of a sunflower.

Getting ready

To generate a hexbin plot, we will use the hexbin package in R along with the generic rnorm() function.

How to do it…

The install.packages() and library() functions assist in installing the package as well as loading it in R:

install.packages("hexbin")
library(hexbin)

For the purpose of this recipe, we will generate a fake dataset. The set.seed(356) code sets the state of random number generation in R:

set.seed(356)

We will implement the rnorm() function to generate 1000 normally distributed random numbers:

x = rnorm(1000)
y = rnorm(1000)

In order to generate a hexbin plot, we will have to define a hexbin object using the hexbin() function:

bins = hexbin...