A way to draw the curve corresponding to a given function is this:
fun1 <- function(x) sin(cos(x)*exp(-x/2))
plot (fun1, -8, 5)
How can I add another function's curve (e.g. fun2, which is also defined by its mathematical formula) in the same plot?
To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.
Learn, how to plot two graphs in a same plot in R Language. To plot the two graphs in the same plot, we can use the par() function in R language. Similarly, you can also use the lines() or points() function to add the graph to an existing plot.
To create a plot that spans multiple rows or columns, specify the span argument when you call nexttile . For example, create a 2-by-2 layout. Plot into the first two tiles. Then create a plot that spans one row and two columns.
plot (fun2, -8, 5, add=TRUE)
Check also help page for curve
.
Using matplot
:
fun1<-function(x) sin(cos(x)*exp(-x/2))
fun2<-function(x) sin(cos(x)*exp(-x/4))
x<-seq(0,2*pi,0.01)
matplot(x,cbind(fun1(x),fun2(x)),type="l",col=c("blue","red"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With