chad.mills
2012-Mar-27 05:17 UTC
[R] Plot of function seems to cut off near edge of domain
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.
Matthieu Dubois
2012-Mar-27 07:31 UTC
[R] Plot of function seems to cut off near edge of domain
Dear Chad, your problem is linked to (1) the function returning NaNs from x values greater than 50, and (2) the fact that the function is estimated on a predefined number of points. Calling plot for a function object is basically a wrapper for curve(). Your function g() is evaluated on the whole xlim domain, which will return NaN values for x>50 (Try g(60) ). In addition, curve() splits the x interval (here from 0 to 60) into a predifined number of points (n=101 is the default, see help(curve)) at which the function is estimated. In your code, the function is estimated at values x <- seq(0, 60, length=101), and g(x) that are not NaN are plotted. The largest x value (from the sequence) that doesn't return a NaN is max(x[!is.nan(g(x))]), which is 49.8. One way to solve it is to explicitly specify the domain used to estimate the function, by using the from and to arguments that are passed to curve(): #Figure 2, with xlim beyond the radius of the circle plot(g,axes=F,from=0, to =50, xlim=c(0, 60), ylim=c(0,60)) axis(1,pos=0) axis(2,pos=0) HTH Matthieu Matthieu Dubois Post-doctoral researcher Psychology Department Universit? Libre de Bruxelles