Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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

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

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...