Is there an equivalent method for plotting functions using ggplot
to the curve()
command employed in base graphics? I guess that the alternative would be to just create a vector of values of the function and plot a connected line, but I was hoping for something a bit more simple.
Thanks!
The
data.frame
example above works well, and it makes grid lines. Theqplot
example doesn't work in ggplot2 2.2.0 for the reasons given.You can also use the "curve" function in ggplot2 2.2.0, but it does not automatically make grid lines or background color. For example:
The "ggplot(data.frame(... ) method gives the full impressive range of ggplot2's formatting options. I like it.
You can add a curve using the
stat_function
:If your curve function is more complicated, then use a lambda function. For example,
you can find other examples at http://kohske.wordpress.com/2010/12/25/draw-function-without-data-in-ggplot2/
In earlier versions, you could use
qplot
, as below, but this is now deprecated.