John Westbury
2010-Jan-04 20:13 UTC
[R] how to plot multiple density functions in one graph
Hello, I am new to R and have two easy questions. How can you plot multiple density functions in one graph? I have five beta densities that I would like to plot in one graph. I understand how to plot one beta density as a line: plot (x,(dbeta(x,shape1=,shape2=,), type ="l") Does the Pareto distribution need to be added to R with an additional package? thanks, John [[alternative HTML version deleted]]
?lines ?points --- On Mon, 1/4/10, John Westbury <jrwestbury at gmail.com> wrote:> From: John Westbury <jrwestbury at gmail.com> > Subject: [R] how to plot multiple density functions in one graph > To: r-help at r-project.org > Received: Monday, January 4, 2010, 3:13 PM > Hello, > > I am new to R and have two easy questions. > > How can you plot multiple density functions in one > graph?? I have five beta > densities that I would like to plot in one graph.? I > und ParetoAdventures of Dunsterforceerstand how to plot > one beta density as a line: > > plot (x,(dbeta(x,shape1=,shape2=,), type ="l") > > Does the distribution need to be added to R with an > additional > package?Maybe. Have look at http://finzi.psych.upenn.edu/R/library/PtProcess/html/dpareto.html for one possible source. __________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/
David Winsemius
2010-Jan-04 20:38 UTC
[R] how to plot multiple density functions in one graph
On Jan 4, 2010, at 3:13 PM, John Westbury wrote:> Hello, > > I am new to R and have two easy questions. > > How can you plot multiple density functions in one graph? I have > five beta > densities that I would like to plot in one graph. I understand how > to plot > one beta density as a line: > > plot (x,(dbeta(x,shape1=,shape2=,), type ="l")?lines> > Does the Pareto distribution need to be added to R with an additional > package? >Sort answer, yes: I see it in the actuar and the VGAM packages that I have installed, but a more complete list of options would be from this search: RSiteSearch("Pareto") David Winsemius, MD Heritage Laboratories West Hartford, CT
Dennis Murphy
2010-Jan-05 00:28 UTC
[R] how to plot multiple density functions in one graph
Hi: Another approach to multiple plotting in base graphics is to use matplot, which is particularly convenient for something such as densities because the plots are of a similar character with the same domain. Here's an example: x <- seq(0, 1, by = 0.01) d1 <- dbeta(x, 2, 2) d2 <- dbeta(x, 3, 3) d3 <- dbeta(x, 4, 4) d4 <- dbeta(x, 5, 5) d5 <- dbeta(x, 6, 6) betapdfs <- rbind(d1, d2, d3, d4, d5) # combine into matrix matplot(x, t(betapdfs), type = 'l', col = 1:5, ylab = 'Density') legendpars <- c('(2, 2)', '(3, 3)', '(4, 4)', '(5, 5)', '(6, 6)') # labels for legend legend('topright', legendpars, lty = 1:5, col = 1:5) You need to transpose the betapdfs matrix so that the number of rows of the 'y' matrix matches the length of x. You may want to tweak some of the features; e.g., to make all lines solid, insert lty = 1 in the matplot call (and in the legend). For anything else, see ?matplot HTH, Dennis On Mon, Jan 4, 2010 at 12:13 PM, John Westbury <jrwestbury@gmail.com> wrote:> Hello, > > I am new to R and have two easy questions. > > How can you plot multiple density functions in one graph? I have five beta > densities that I would like to plot in one graph. I understand how to plot > one beta density as a line: > > plot (x,(dbeta(x,shape1=,shape2=,), type ="l") > > Does the Pareto distribution need to be added to R with an additional > package? > > thanks, > John > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]