1. ## Function to shade functions (typically pdfs)

Hey guys. I got bored and a thread reminded me that I wanted to make a function to do generic shading of distribution functions. The code could probably be a little bit better but hopefully this could help a few people out.

Code:
``````shadeplot <- function(func = dnorm, xlim = c(-3,3),
ylim = NULL, shadelim = c(2,Inf),
col = "red", xlab = "", ylab = "", main = "", ...)
{
## func    : The density function to be plotted
## xlim    : The x-limits for the plotting region
## ylim    : The y-limits for the plotting region
##             - If left as NULL then the default height
##             - will be used and the lower limit will be
##             - set to 0
##             - Testing has not been on anything other than
##             - ylim = c(0, some_other_maximum_value)
## col     : The color to shade the region
## xlab    : Label for the x-axis
## ylab    : Label for the y-axis
## main    : Plot title
## ...     : Additional parameters to be passed on to func
xs <- seq(xlim[1], xlim[2], length.out = 1000)
ys <- func(xs, ...)

# Make sure plot has lower bound of y = 0
xy <- xy.coords(xs, ys)
if (is.null(ylim)){
ylim <- range(xy\$y[is.finite(xy\$y)])
ylim[1] <- 0
}

# Plot the function
plot(xs, ys, type = "l", xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab, main = main)

# Find which xs are inside of our bounds
idx <- ((xs >= lb) & (xs <= ub))

polygon(c(lb, lb, xs[idx], ub, ub), c(0, func(lb, ...), ys[idx], func(ub, ...), 0), col = col)
}``````
Code:
``````#################################
### Testing: Examples follow
#################################

# Defaults

# F distribution
shadeplot(df, xlim = c(0,5), shadelim = c(2.4, Inf), df1 = 4, df2 = 20)

# Watch a t converge to a normal
for(i in 1:50){
shadelim = c(1.96,Inf), df = i,
main = paste("DF: ",i), ylim = c(0, .45),
xlab = paste("P(T > 2) = ", round(1-pt(2, df = i), 4))
)
Sys.sleep(.08)
}``````
Edit: On second thought it would probably be better to just create a function to shade a plot after the fact. Similar to adding points or lines... Maybe I'll do that later. That way I could remove some of the unnecessary plotting parameters (xlab, ylab, main) and it would follow a more unix-y philosophy (which is great).

Second Edit: I renamed the thread because technically this can shade things other than pdfs. However, it will always set the lower y-limit to 0 so that could be an issue. So as long as you're working with a non-negative function (or are at least only plotting a non-negative section) it should be fine.

2. ## The Following 2 Users Say Thank You to Dason For This Useful Post:

Nathan G (03-08-2013), trinker (03-08-2013)

3. ## Re: Function to shade functions (typically pdfs)

BUMP. Mainly because I didn't have this code saved in my dropbox for some reason and I just searched the entire "share your code" thread looking for this guy.

4. ## Re: Function to shade functions (typically pdfs)

Didn't see this. This could be a useful teaching tool. Dason could you post the link in the share your functions and code so the record is easier to find?

5. ## Re: Function to shade functions (typically pdfs)

I do get an error here: > shadeplot(df, xlim = c(0,5), shadelim = c(2.4, Inf), df1 = 4, df2 = 20)
Code:
``````> for(i in 1:50){
+      shadelim = c(1.96,Inf), df = i,
+      main = paste("DF: ",i), ylim = c(0, .45),
+      xlab = paste("P(T > 2) = ", round(1-pt(2, df = i), 4))
+   )
+   Sys.sleep(.08)
+ }
Error in shadeplot(dt, xlim = c(-3, 3), shadelim = c(1.96, Inf), df = i,  :
could not find function "func"``````

6. ## Re: Function to shade functions (typically pdfs)

Originally Posted by trinker
Didn't see this. This could be a useful teaching tool. Dason could you post the link in the share your functions and code so the record is easier to find?
If I remember to tomorrow then I will. But what I really should do is add it to Dmisc

7. ## Re: Function to shade functions (typically pdfs)

Originally Posted by trinker
I do get an error here: > shadeplot(df, xlim = c(0,5), shadelim = c(2.4, Inf), df1 = 4, df2 = 20)
Code:
``````> for(i in 1:50){
+      shadelim = c(1.96,Inf), df = i,
+      main = paste("DF: ",i), ylim = c(0, .45),
+      xlab = paste("P(T > 2) = ", round(1-pt(2, df = i), 4))
+   )
+   Sys.sleep(.08)
+ }
Error in shadeplot(dt, xlim = c(-3, 3), shadelim = c(1.96, Inf), df = i,  :
could not find function "func"``````
Interesting... it runs fine for me. Did you copy/paste something wrong?

8. ## Re: Function to shade functions (typically pdfs)

No. Not sure what's up. I'll try running a vanilla R. This fixes it though:

Code:
``````  func <-  match.fun(func)
ys <- func(xs, ...)``````

9. ## Re: Function to shade functions (typically pdfs)

Must be something I have assigned to something in my global envir. In a vanilla session it runs fine.

10. ## Re: Function to shade functions (typically pdfs)

One could also use clip function for this.

Code:
``````for(i in 1:50){
xxlim <- seq(-3,3,length.out=1000)
plot(xxlim,dt(x=xxlim,df=i),xlim = c(-3,3),type="l", main = paste("DF: ",i), ylim = c(0, .45),
xlab = paste("P(T > 2) = ",round(1-pt(2, df = i), 4)))
clip(x1=2,x2=3,y1=0,y2=0.45)
polygon(xxlim,dt(x=xxlim,df=i),xlim = c(-3,3),type="l", main = paste("DF: ",i), ylim = c(0, .45),
xlab = paste("P(T > 2) = ",round(1-pt(2, df = i), 4)), col="red",add=TRUE )
Sys.sleep(.08)
}``````
Dason, Thanks for your idea. I am using Your example in my presentation.

 Tweet

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts