[R] Problem with ?curve
Greg Snow
Greg.Snow at intermountainmail.org
Thu Apr 19 19:33:43 CEST 2007
Something along these lines is on my todo list for the TeachingDemos
package, so any thoughts on what you want the user interface to be would
be helpful (do you want sliders to set the x-value? Do you want to click
on the plot to see an update? Or just moving the mouse over the plot
without clicking would update the line?).
If you don't mind clicking on the plot, here is a quick example of what
you could do (put in your own function and numbers):
library(TeachingDemos)
myfun <- function(x) sqrt(x)
curve( myfun, 0, 10 )
tmp <- cnvrt.coords(0,0, input='plt')$usr
tmp2 <- cnvrt.coords(-0.05, -0.05, input='plt')$usr
par(xpd=TRUE)
while( length(myx <- locator(1)$x) ){
myy <- myfun(myx)
curve( myfun, 0, 10 )
lines( c(myx,myx,tmp$x), c(tmp$y, myy, myy), col='green' )
text( c( myx, tmp2$x ), c(tmp2$y, myy), round(c( myx, myy ), 2),
col='red')
}
If you run the above code it will create the plot, then wait for you to
click on the plot, when you click it will draw the lines corresponding
to the x location of your click and put labels in the margins, clicking
again will move the lines and update the labels. Right click and select
'stop' when you are through.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ron Michael
> Sent: Thursday, April 19, 2007 1:16 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Problem with ?curve
>
> Dear all R users,
>
> Thank you Gabor, and Mark for spending some times on my
> problem. But I want to move little bit further. I want to
> show some basic features interactively. I want to move my
> mouse pointer through the X axis. In graphics window I want
> to show that, for a particular position of pointer in X-axis,
> there will be a vertical straightline from that position,
> which will cull the curve at a point, and from this point
> another horizontal straight line will be there, which will
> cut to Y-axis at a particular point. And upon moving my
> pointer along X-axis, that mapping-point in Y-axis will also
> move along y-axis accordingly. I also want to mark the
> position of X-axis point and Y-axis point interactively.
>
> I know there is a package "TeachingDemos" for creating this
> type of interactive picture. But I really dont know how to
> apply this in my problem.
>
> Can anyone give me any suggestion? Your help will be highly
> appreciated.
>
> Thanks and regards,
>
>
> ----- Original Message ----
> From: Gabor Grothendieck <ggrothendieck at gmail.com>
> To: Ron Michael <ron_michael70 at yahoo.com>
> Cc: r-help at stat.math.ethz.ch
> Sent: Wednesday, April 18, 2007 10:08:32 PM
> Subject: Re: [R] Problem with ?curve
>
>
> Your chipply function is not vectorized. See ?curve and try:
>
> curve(Vectorize(chippy)(x), 1, 20, n=200)
>
>
> On 4/18/07, Ron Michael <ron_michael70 at yahoo.com> wrote:
> >
> > Dear all R gurus,
> >
> >
> >
> > I have following syntax:
> >
> >
> >
> > y = c(1:10)
> > chippy <- function(x)
> > {
> > y[5] = x
> > sin(cos(t(y)%*%y)*exp(-t(y)%*%y/2))
> > }
> > curve(chippy, 1, 20, n=200)
> >
> >
> >
> > But I am getting error while executing :
> >
> >
> >
> > Error in xy.coords(x, y, xlabel, ylabel, log) :
> > 'x' and 'y' lengths differ
> > In addition: Warning message:
> > number of items to replace is not a multiple of replacement length
> >
> >
> > Can anyone tell me how I can recover?
> >
> >
> >
> > Thanks
> >
> > Ron
> >
> >
> >
> >
> >
> >
> >
> >
> > Send instant messages to your online friends
> http://uk.messenger.yahoo.com
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
>
> Send instant messages to your online friends
> http://uk.messenger.yahoo.com
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
More information about the R-help
mailing list