Charlie - 1 year ago 85

R Question

Is there an equivalent method for plotting functions using

`ggplot`

`curve()`

Thanks!

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can add a curve using the `stat_function`

:

```
ggplot(data.frame(x=c(0, 10)), aes(x)) + stat_function(fun=sin)
```

It is also possible to use `qplot`

, but it's not clear if this is simplier:

```
qplot(c(0,2), fun=sin, stat="function", geom="line")
```

If your curve function is more complicated, then use a lambda function. For example,

```
ggplot(data.frame(x=c(0, 10)), aes(x)) +
stat_function(fun=function(x) sin(x) + log(x))
```

you can find other examples at http://kohske.wordpress.com/2010/12/25/draw-function-without-data-in-ggplot2/

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**