Creating shaded areas in R

May 12, 2005

(This article was first published on - en » R, and kindly contributed to R-bloggers)

Sometimes we want to shade areas under a density on a graphic, for instance to illustrate a p-value or a region under the normal curve.

Using the polygon() function from base R we can make graphics like the following one:

Standard normal density. Shaded area shows P(1 < X < 2)

How to do it

First we have to plot the graphic of the function whose areas we are going to shade. We can usually do that using the function curve(). When we have a standard normal density we can use:

View Code RSPLUS

The command above will create the graphic over an arbitrary range on the x values though. Since we want to shade a specific area under the curve, it is advisable to specify which interval on the x-axis we want the function plotted on. We can do that using the xlim parameter:

View Code RSPLUS
curve(dnorm(x,0,1),xlim=c(-3,3),main='Normal Density')

Now that we have our curve plotted, we are going to use the polygon(x,y) function. Its most important parameters are x and y, which define what kind of polygon R is going to draw. If x and y are vectors with i=1,…n elements, polygon(x,y) draws a polygon with vertices (xi,yi), i=1,…n. The trick now consists of expressing our region of interest as a polygon. The idea might seem stupid at first, as the area of interest is usually over a continuous curve, but if we take a large enough number of vertices the approximation will be very reasonable.

Let’s say we want to shade the region represented by P(-3 < X < -2). The first vertex we want for our polygon is (-3,0). We have then:

View Code RSPLUS
cord.x <- c(-3)
cord.y <- c(0)

The second vertex will be (-3,f(-3)), where f(-3) is the normal density evaluated at -3. We can readily obtain this value on R using the dnorm() function:

View Code RSPLUS
cord.x <- c(cord.x,-3) 
cord.y <- c(cord.y,dnorm(-3))

As a third and fourth vertices we can consider (-2,f(-2)) and (-2,0). We have now:

View Code RSPLUS
cord.x <- c(cord.x,-2,-2)
cord.y <- c(cord.y,dnorm(-2),0)

We then issue:

View Code RSPLUS

Obtaining (if you followed our instructions correctly ):

First attempt at creating a shaded area in R

Not that bad, but we can make it better than that. We just have to create a sequence of vertices between the two extrema of the density (f(-3) and f(-2) here), so that the larger number of vertices gives us a better approximation. In order to accomplish that we are going to use the seq() function, to create a sequence between -3 and 2, with steps of 0.01:

View Code RSPLUS
 cord.x <- c(-3,seq(-3,-2,0.01),-2) 
 cord.y <- c(0,dnorm(seq(-3,-2,0.01)),0) 
 curve(dnorm(x,0,1),xlim=c(-3,3),main='Standard Normal') 

Which gives us the result:

Final graphic.


Note that we used the standard normal density. If we wanted to use another probability distribution its parameters would have to be properly specified. Try playing with areas under the exponential distribution, for example. To learn more about the uses and misuses of the polygon function, take a look at its documentation (?polygon).

flattr this!

To leave a comment for the author, please follow the link and comment on their blog: - en » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training



CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)