[R] plot pdf

Joshua Wiley jwiley.psych at gmail.com
Sat Oct 30 02:49:57 CEST 2010


Hi Mary,

I am not sure off hand why savePlot() is not doing what you expect,
but hopefully my inline comments answer your other questions and at
least get you the graphs you want.

Cheers,

Josh

On Fri, Oct 29, 2010 at 3:18 PM,  <mmstat at comcast.net> wrote:
>
>
> I want to plot the unstadardized version of a normal plot.  Can you explain why that is not working?
> Dev.set(1)
> xcrit=-1.645
> cord.x <- c(-3,seq(-3,xcrit,0.01),xcrit)
> cord.y <- c(0,dnorm(seq(-3,xcrit,0.01)),0)   # what does final 0 do here?

You are creating *coordinates* to draw a polygon.  That is, (x, y).
The 0s just match with -3 and xcrit and serve to define the perimeter
of the polygon.  Please read the documentation for ?polygon for more
details.

> curve(dnorm(x,0,1),xlim=c(-3,3),main='Normal PDF')

This makes your nice curve based on the density function for the
normal distribution with a range from -3 to +3.  This part works fine
for me.

> polygon(cord.x,cord.y,col='orange')

This does the coloring, the coordinates define the polygon, which is
closed by connecting the first and last points.  This also seems to
work.  I end up with the lower region of the graph filled orange.

> savePlot(filename="c:\\ssl_z.emf",type="emf")          #unable to create metafile?  why?

hard for me to say right now (I'm on a linux box).  It might help
though if you reported sessionInfo()

> x=seq(80,200,1)
> plot(x,dnorm(x,140,15),type="l",ylab="Probability")

This seems fine to me also.

> dev.set(4)

> xcrit=135.9
> cord.x <- c(80,seq(80,xcrit,.1),xcrit)
> cord.y <- c(0,dnorm(seq(80,xcrit,.1)),0)              #final 0 needs to be changed but to what?

Why does the final zero need to be changed?  What do you want it to
be?  I suspect you do need to change dnorm() though.  You are creating
y coordinates based on a normal distribution with mean 0 and sd = 1,
but then you plot a curve for a mean of 140 and sd = 15.  I would
adjust your code to:

cord.y <- c(0, dnorm(seq(80, xcrit, .1), 140, 15), 0)

which gives the results I *think* you are after.


> curve(dnorm(x,140,15),xlim=c(80,200),main='Normal PDF',ylab="Probability")
> polygon(cord.x,cord.y,col='orange')

Using the adjustment I showed above, I now get a graph that is filled
orange from 80 to xcrit (under the normal curve).

> savePlot(filename="c:\\ssl_z.emf",type="emf")         #unable to create metafile?  why?
>
>
> Sincerely,
>
> Mary A. Marion
>
>
>        [[alternative HTML version deleted]]

I manually removed spaces in my reply.  In the future it nice if you
could send plain text emails (since the html ones are scrubbed).

>
>
> ______________________________________________
> R-help at 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list