[R] Distribution Graph Idea/Help

Derek Ogle DOgle at northland.edu
Sat Apr 19 01:16:37 CEST 2008


For pedagogical reasons (i.e., moving a way from tables of distributions) I want to plot a probability density or distribution function with a shaded area corresponding to the calculated value or user input.  For example, I want a plot to visually demonstrate the result of 

pnorm(1.3)

I have seen the interactive examples in TeachingDemos and rPanel but am interested in using the function from the "command line" rather than from a "GUI".  I have generated code to construct this type of graph, but my question is now how best to implement it.  My initial solution was to add a plot=TRUE argument to pnorm() but that requires altering a base function and I now think that this is not a good idea (although it worked nicely from the student's perspective).  Thus, I have considered three new options ...

1) making a new function called something like pnorm1() which would return what pnorm() returns but also would construct the graphic.  This implementation would be simple given the function that I have already made but it feels clumsy and inelegant.
2) creating a generic plot() function that would take, for example, an object returned from pnorm().  However, this does not seem immediately possible as, it appears to me, that pnorm() just returns a numeric.
3) creating a general function that would take the name of a distribution, type of calculation ("p" or "q" type), value of interest (x value or probability), and distribution parameters as arguments and then call the specific distribution functions and my graphing function to produce the results.  I would likely implement this just for distributions that I use regularly in my classes.

Has anybody already implemented a solution to this idea?  Does anyone have a suggestion on which of my three options above is best for this code?  Better ideas?

Thanks in advance for any help.



More information about the R-help mailing list