Graphics Reference
In-Depth Information
what to return, conditional on the value of x . But that won't work here, since x is a vector with
many values.
Discussion
R has first-class functions, and we can write a function that returns a closure—that is, we can
program a function to program another function.
This function will allow you to pass in a function, a minimum value, and a maximum value. Val-
ues outside the range will again be returned with NA :
limitRange <- function
function (fun, min, max) {
function
function (x) {
y <- fun(x)
y[x < min | x > max] <- NNA
return
return (y)
}
}
Now we can call this function to create another function—one that is effectively the same as the
dnorm_limit() function used earlier:
# This returns a function
dlimit <- limitRange(dnorm, 0 , 2 )
# Now we'll try out the new function -- it only returns values for inputs
# between 0 and 2
dlimit( -2 : 4 )
[ 1 ]
NA
NA 0.39894228 0.24197072 0.05399097
NA
NA
We can use limitRange() to create a function that is passed to stat_function() :
p + stat_function(fun = dnorm) +
stat_function(fun = limitRange(dnorm, 0 , 2 ),
geom = "area" , fill = "blue" , alpha = 0.2 )
The limitRange() function can be used with any function, not just dnorm() , to create a range-
limited version of that function. The result of all this is that instead of having to write functions
with different hardcoded values for each situation that arises, we can write one function and
simply pass it different arguments depending on the situation.
If you look very, very closely at the graph in Figure 13-6 , you may see that the shaded region
does not align exactly with the range we specified. This is because ggplot2 does a numeric ap-
proximation by calculating values at fixed intervals, and these intervals may not fall exactly with-
Search WWH ::




Custom Search