Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating kernel density plots


Let's create a kernel density plot for patient height. The kernel density plot is essentially a smoothed version of a histogram. A full discussion of kernel density plots is beyond the scope of this book, but for many applications they provide a viable alternative to the histogram. We use a bin width of 5 cm, though we could try other bin widths. Enter the following syntax:

qplot( HEIGHT, data = T,   geom = "density",  binwidth = 5, xlab = "HEIGHT (cm)", ylab = "DENSITY")  

The kernel density plot looks like this:

In a kernel density plot, the height of the curve gives an estimate of the probability density at the given value along the horizontal axis.

To shade underneath a density plot, you can use the polygon() command. In the following example, we will illustrate how this is done in base graphics. Here, we select a light green color and use the default smoothing (that is, we do not specify a bin width). Enter the following syntax:

plot(density(HEIGHT), xlab...