Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

This book is targeted at R programmers who want to learn the graphing capabilities of R. This book will presume that you have working knowledge of R.
Table of Contents (6 chapters)
5
Index

Including smoothed curves


Let's create a scatterplot relating height and weight before treatment, along with both points and a smooth curve using geom = c("point","smooth"). In qplot, the default smoother is LOWESS, and the gray band represents a standard error confidence interval. LOWESS fits models to local subsets of the variables to produce a smoothed version of the data.

You can read further about LOWESS in various texts and online sources. For this example, we set up the graph as an object (Y) and plot it by entering the object name on the command line:

Y <- qplot(HEIGHT, WEIGHT_1, data = T, xlab = "HEIGHT (cm)", ylab = "WEIGHT BEFORE TREATMENT (kg)", geom = c( "point","smooth"))   
 
Y

Our graph now looks like this:

We have the smoothed curve and the confidence interval. Let's graph the same data, but map color to ethnicity. We add transparency in order to make the curves easy to interpret. The syntax is as follows:

Y <- qplot(HEIGHT, WEIGHT_1, data = T, xlab = "HEIGHT (cm)",...