0 votes
1 view
in R Programming by (5.3k points)

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!

1 Answer

0 votes
by (25.3k points)

To add curves in ggplot, you can use the stat_function as follows:

library("ggplot2")

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

To add complex curves, you can use the lambda function as follows:

ggplot(data.frame(x=c(0, 10)), aes(x)) + 

  stat_function(fun=function(x) sin(x) + log(x))

image

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...