Back

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

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

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...