Making scatter plots with smoothed density representation

Smoothed density scatter plots are a good way of visualizing large datasets. In this recipe, we will learn how to make them using the smoothScatter() function.

Getting ready

For this recipe, we don't need to load any additional libraries. We just need to type the recipe at the R prompt or run it as a script.

How to do it...

We will use the smoothScatter() function which is part of the base graphics library. We will use an example from the help file which can be accessed from the R prompt with the help command:

n <- 10000
x  <- matrix(rnorm(n), ncol=2)
y  <- matrix(rnorm(n, mean=3, sd=1.5), ncol=2)
smoothScatter(x,y)
How to do it...

How it works...

The smoothScatter() function produces a smoothed color density representation of the scatter plot, obtained through a kernel density estimate. We passed the x and y variables which represented the data to be plotted. The gradient of the blue color shows the density of the data points, with most points in the center of the graph. The dots in the outer light blue circles are outliers.

There's more...

We can pass a number of arguments to smoothScatter() to adjust the smoothing, for example nbin for specifying the number of equally spaced grid points for the density estimation, and nrpoints to specify how many points to show as dots. In addition, we can also pass standard arguments such as xlab, ylab, pch, cex, and so on to modify axis and plotting symbol characteristics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset