here to make pretty scatter plots of correlated variables with ggplot2!

We’ll learn how to create plots that look like this:

init-example-1.png

 Data

In a data.frame

d

, we’ll simulate two correlated variables

a

and

b

of length

n

:

 Basic scatter plot

Using ggplot2, the basic scatter plot (with

theme_minimal

) is created via:

unnamed-chunk-3-1.JPEG

 Shape and size

There are many ways to tweak the

shape

and

size

of the points. Here’s the combination I settled on for this post:

unnamed-chunk-4-1.JPEG

 Color

We want to color the points in a way that helps to visualise the correlation between them.

One option is to

color

by one of the variables. For example, color by

a

(and hide legend):

unnamed-chunk-5-1.JPEG

Although it’s subtle in this plot, the problem is that the color is changing as the points go from left to right. Instead, we want the color to change in a direction that characterises the correlation – diagonally in this case.

To do this, we can color points by the first principal component. Add it to the data frame as a variable

pc

and use it to color like so:

unnamed-chunk-6-1.JPEG

Now we can add color, let’s pick something nice with the help of the

scale_color_gradient

functions and some nice hex codes (check out color-hex for inspriation). For example:

unnamed-chunk-7-1.JPEG

 Transparency

Now it’s time to get rid of those offensive mushes by adjusting the transparency with

alpha

.

We could adjust it to be the same for every point:

unnamed-chunk-8-1.JPEG

This is fine most of the time. However, what if you have many points? Let’s try with 5,000 points:

unnamed-chunk-9-1.JPEG

We’ve got another big mush. What if we take

alpha

down really low to .05?

unnamed-chunk-10-1.JPEG

Better, except it’s now hard to see extreme points that are alone in space.

To solve this, we’ll map

alpha

to the inverse point density. That is, turn down

alpha

wherever there are lots of points! The trick is to use bivariate density, which can be added as follows:

Now plot with

alpha

mapped to

1/density

:

unnamed-chunk-12-1.JPEG

You can see that distant points are now too vibrant. Our final fix is to use

scale_alpha

to tweak the alpha range. By default, this range is 0 to 1, making the most distant points have an alpha close to 1. Let’s restrict it to something better:

unnamed-chunk-13-1.JPEG

Much better! No more mushy patches or lost points.

 Bringing it together

Here’s a complete example with new data and colors:

unnamed-chunk-14-1.png

 Sign off

Thanks for reading and I hope this was useful for you.

For updates of recent blog posts, follow @drsimonj on Twitter, or email me at drsimonjackson@gmail.com to get in touch.

If you’d like the code that produced this blog, check out the blogR GitHub repository.

 

소스: Pretty scatter plots with ggplot2 | R-bloggers