Quantcast

To make a graph for 4 functions

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

To make a graph for 4 functions

quantum
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: To make a graph for 4 functions

Rui Barradas
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: To make a graph for 4 functions

quantum
In reply to this post by quantum
Thanks for your response.

Now I have 3 functions for example

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

t<-function(x) c(y0(x),y1(x),y2(x))

I want to plot this so I get 3 graphs in a diagram.

How can I do this?

plot(x,t(x),type="l") doesnt work.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: To make a graph for 4 functions

Michael Weylandt
Use curve() like Rui said.

Michael

On Fri, Apr 27, 2012 at 1:48 PM, quantum <[hidden email]> wrote:

> Thanks for your response.
>
> Now I have 3 functions for example
>
> y0<-function(x) x^2
> y1<-function(x) x^3
> y2<-function(x) x^5
>
> t<-function(x) c(y0(x),y1(x),y2(x))
>
> I want to plot this so I get 3 graphs in a diagram.
>
> How can I do this?
>
> plot(x,t(x),type="l") doesnt work.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/To-make-a-graph-for-4-functions-tp4592941p4593316.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: To make a graph for 4 functions

quantum
How can I use the curve when I have a vector? How should the R code look like?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: To make a graph for 4 functions

Rui Barradas
Hello,

quantum wrote
How can I use the curve when I have a vector? How should the R code look like?
You can't. From the manual:

"expr The name of a function, or a call or an expression written as a function of x which will evaluate to an object of the same length as x."

Your function does not return an object of the same length as x. It triples it's length.
You are complicating what is simple and was already answered.

Your latest function, t, has that problem.
A second problem is that it returns a vector when to make some sense it should return a matrix.
?cbind

A third problem is the name 't', it is the name of R's matrix transpose function.

Tip: Plot each curve one at a time like said before.

Rui Barradas
Loading...