Graphics Reference
In-Depth Information
p
+
stat_function(fun
=
dnorm)
Figure 13-4. Left: the normal distribution; right: the t-distribution with df=2
Discussion
Some functions take additional arguments. For example,
dt()
, the function for the density of the
t-distribution, takes a parameter for degrees of freedom (
Figure 13-4
, right). These additional
arguments can be passed to the function by putting them in a list and giving the list to
args
:
p
+
stat_function(fun
=
dt, args
=
list(df
=
2
))
It's also possible to define your own functions. It should take an xvalue for its first argument,
and it should return a yvalue. In this example, we'll define a sigmoid function (
Figure 13-5
):
myfun
<-
function
function
(xvar) {
1
/
(
1
+
exp(
-
xvar
+
10
))
}
ggplot(data.frame(x
=
c(
0
,
20
)), aes(x
=
x))
+
stat_function(fun
=
myfun)