[R] Plot of function seems to cut off near edge of domain

2012-03-27 Thread chad.mills
Hello helpful R folks,
I am simply trying to graph a quarter circle centered at the origin in the
first quadrant.  When I set the xlim of the plot to the radius of the
circle, the plot appears correct.  However, I'd like to see a slight
extension of the axes beyond the domain of the function itself.  When I do
this, a portion of the plot seems to be missing by the edge of the domain. 
Here is the code for both of the plots:

dev.off() 
plot.new()
#Set up two-figure plot
par(mfrow=c(1,2),pty='s')
g-function(x){sqrt(2500-x^2)}
#Figure 1, with xlim at the radius of the circle
plot(g,axes=F,xlim=c(0,50),ylim=c(0,50))
axis(1,pos=0)
axis(2,pos=0)
#Figure 2, with xlim beyond the radius of the circle
plot(g,axes=F,xlim=c(0,60),ylim=c(0,60))
axis(1,pos=0)
axis(2,pos=0)

Notice that the second graph doesn't appear to intersect the x-axis, while
the first one does.  Any ideas why that might be the case?  Here's an image
of what I see in case that's useful:

http://r.789695.n4.nabble.com/file/n4507954/Cut_off_Quarter_Circle.png 

Thanks in advance for the help!

-Chad Mills

--
View this message in context: 
http://r.789695.n4.nabble.com/Plot-of-function-seems-to-cut-off-near-edge-of-domain-tp4507954p4507954.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


[R] Simple question regarding domain restrictions/piecewise functions in R

2012-03-26 Thread chad.mills
I am a novice R user.

I would like to be able to graph some simple piecewise functions/functions
with domain restrictions in R, but I'm having trouble defining such
functions.  For example, I would like to define the following function:

f(x)={x^2 if -1xx; 1 if 2x3}

Notably, the function is undefined outside of domain (-1,1)U(2,3).  My best
attempt in R is something like this:

f-function(x) ifelse(-1x  x1,x^2,ifelse(2=x  x=3,1,as.null())) 

This approach works fine for values within the domain.  But, naturally, when
I try values outside of the domain I get an error.  Consequently, when I try
to graph such a function:

plot(f)

I get the following error: 

Error in ifelse(2 = x  x = 3, 1, as.null()) : 
  replacement has length zero

I realize that I could create vectors of x-y points and plot using the
plot(x,y) command, but it seems to me that I should be able to define a
piecewise function symbolically and just graph it.  Am I wrong?  Any help
would be greatly appreciated.

--
View this message in context: 
http://r.789695.n4.nabble.com/Simple-question-regarding-domain-restrictions-piecewise-functions-in-R-tp4504199p4504199.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Simple question regarding domain restrictions/piecewise functions in R

2012-03-26 Thread chad.mills
Yes!  Thanks.  It was just the NA value instead of the as.null that does
the trick.  Correct code for the original piecewise I stated (for those who
might be looking later) is:

f - function(x){ 
 ifelse((-1  x  x  1),x^2,ifelse((2xx3),1,NA)) 
} 
 plot(f,xlim=c(-1,3)) 

-Chad Mills

--
View this message in context: 
http://r.789695.n4.nabble.com/Simple-question-regarding-domain-restrictions-piecewise-functions-in-R-tp4504199p4506155.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.