Hello, i have written this little function to draw different normal distributions: n.Plot <- function(x,my,sigma) { e <- exp(1) names(x) <- x f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) plot(f.x,type="l",xlim=c(-5,5)) return(f.x) } if i define x like this: x <- seq(-5,5,0.01) Now n.Plot(x,0,1) DOES draw the correct plot, but the x-axis is labeled from 0 - 1000. If i give the plot function the xlim=c(-5,5) option, the plot is only drawn for the first 5 values of x. How do i tell plot not to take the indices of x, but the values (or their range) of x for labeling the x-axis ? Felix Eschenburg
I think you meant to do plot(x, f.x, ...) BTW, you've re-invented the wheel. See ?dnorm for evaluating the normal pdf. Best, Sundar Felix Eschenburg wrote:> Hello, > i have written this little function to draw different normal distributions: > > n.Plot <- function(x,my,sigma) { > e <- exp(1) > names(x) <- x > f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) > plot(f.x,type="l",xlim=c(-5,5)) > return(f.x) > } > > if i define x like this: > x <- seq(-5,5,0.01) > > Now > n.Plot(x,0,1) > DOES draw the correct plot, but the x-axis is labeled from 0 - 1000. > If i give the plot function the xlim=c(-5,5) option, the plot is only drawn > for the first 5 values of x. > How do i tell plot not to take the indices of x, but the values (or their > range) of x for labeling the x-axis ? > > Felix Eschenburg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
See ?range. And note that I changed plot to more explicitly show what is being plotted. HTH, Andy #~~~~~~~~~~~~~~~~~~ n.Plot <- function(x,my,sigma) { e <- exp(1) names(x) <- x f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) plot(x, f.x, type="l", xlim = range(x)) return(f.x) } n.Plot(seq(-5,5,0.01),0,1) n.Plot(seq(-50,50,0.01),0,0.1)
Atropin75 at t-online.de (Felix Eschenburg) writes:> Hello, > i have written this little function to draw different normal distributions: > > n.Plot <- function(x,my,sigma) { > e <- exp(1) > names(x) <- x > f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) > plot(f.x,type="l",xlim=c(-5,5)) > return(f.x) > } > > if i define x like this: > x <- seq(-5,5,0.01) > > Now > n.Plot(x,0,1) > DOES draw the correct plot, but the x-axis is labeled from 0 - 1000. > If i give the plot function the xlim=c(-5,5) option, the plot is only drawn > for the first 5 values of x. > How do i tell plot not to take the indices of x, but the values (or their > range) of x for labeling the x-axis ?Tell plot what your x coordinates are: plot(x, f.x, type="l", xlim=c(-5,5)) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Thank you all, that did the trick. Sometimes i can be a real blockhead.> Atropin75 at t-online.de (Felix Eschenburg) writes: > > Hello, > > i have written this little function to draw different normal > > distributions: > > > > n.Plot <- function(x,my,sigma) { > > e <- exp(1) > > names(x) <- x > > f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) > > plot(f.x,type="l",xlim=c(-5,5)) > > return(f.x) > > } > > > > if i define x like this: > > x <- seq(-5,5,0.01) > > > > Now > > n.Plot(x,0,1) > > DOES draw the correct plot, but the x-axis is labeled from 0 - 1000. > > If i give the plot function the xlim=c(-5,5) option, the plot is only > > drawn for the first 5 values of x. > > How do i tell plot not to take the indices of x, but the values (or their > > range) of x for labeling the x-axis ? > > Tell plot what your x coordinates are: > > plot(x, f.x, type="l", xlim=c(-5,5))
Felix, there may be more elegant ways of plotting the normal curve, but given your current program, you can simply change your plot statment to use a formula: plot(f.x ~ x, type="l", xlim=c(-5,5)) Dan Nordlund ---------Original message---------- In a message dated 2/5/2004 2:52:04 PM Pacific Standard Time, Atropin75 at t-online.de writes: Hello, i have written this little function to draw different normal distributions: n.Plot <- function(x,my,sigma) { e <- exp(1) names(x) <- x f.x <- (1/(sigma*sqrt(2*pi)))*e^(-1*(((x-my)^2)/2*(sigma^2))) plot(f.x,type="l",xlim=c(-5,5)) return(f.x) } if i define x like this: x <- seq(-5,5,0.01) Now n.Plot(x,0,1) DOES draw the correct plot, but the x-axis is labeled from 0 - 1000. If i give the plot function the xlim=c(-5,5) option, the plot is only drawn for the first 5 values of x. How do i tell plot not to take the indices of x, but the values (or their range) of x for labeling the x-axis ? Felix Eschenburg