[R] To make a graph for 4 functions

Rui Barradas ruipbarradas at sapo.pt
Fri Apr 27 17:59:51 CEST 2012


Hello,


quantum wrote
> 
> lets say I have these function and I want to have a graph on them
> 
> y0=x^2
> y1=x^3
> 
> Then I say this
> 
> x=seq(0,10,length.out=100)
> plot(x,y0,y1,type="l")
> 
> but R does not give me a graph. How would you do it?
> 

First of all, try your own code:

> y0=x^2
Error: object 'x' not found

Unlike you've said, y0=x^2 is not a function. Functions in R look like this:

y0 <- function(x) x^2
y1 <- function(x) x^3

This is chapter 10 of R-intro.pdf, "Writing your own functions".

Also, use the help system.
?plot

It will tell you that 'plot' plot x against y, not against two variables.

plot(x, y0(x), type="l")
lines(x, y1(x))

Or, use function 'curve'.

curve(x^2, from=0, to=10, col="red")
curve(x^3, from=0, to=10, col="blue", add=TRUE)

And, please, read the manual above. It WILL save you time.

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/To-make-a-graph-for-4-functions-tp4592941p4593083.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list