Hi, I wanted to plot the histogram of a vector and then, plot the density function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows: length(b) = 1000 truehist(b) lines(density(b[1:100])) however the density plot of the first 100 points exceeds the max of y axis (see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set? Cheers, Carol
truehist and density plots
9 messages · carol white, Uwe Ligges, Cuvelier Etienne +1 more
carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows: length(b) = 1000 truehist(b) lines(density(b[1:100]))
I do not undertsand what you mean. Can you please provide a *reproducible* example? Uwe Ligges
however the density plot of the first 100 points exceeds the max of y axis (see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set?
Cheers,
Carol
------------------------------------------------------------------------
______________________________________________ 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.
Consider a vector of 100 elements (attached files). then, truehist(b) lines(density(b[20:50])) How is it possible to have density plots of all subsets like b[20:50] within histogram (without exceeding the max of historgram on y axis)? Is it more clear? Best,
--- On Tue, 4/28/09, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote:
From: Uwe Ligges <ligges at statistik.tu-dortmund.de> Subject: Re: [R] truehist and density plots To: "carol white" <wht_crl at yahoo.com> Cc: r-help at stat.math.ethz.ch Date: Tuesday, April 28, 2009, 5:42 AM
carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density
function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows:
length(b) = 1000 truehist(b) lines(density(b[1:100]))
I do not undertsand what you mean. Can you please provide a *reproducible* example? Uwe Ligges
however the density plot of the first 100 points exceeds the max of y axis
(see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set?
Cheers,
Carol
------------------------------------------------------------------------
______________________________________________ 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.
carol white wrote:
Consider a vector of 100 elements (attached files). then, truehist(b) lines(density(b[20:50])) How is it possible to have density plots of all subsets like b[20:50] within histogram (without exceeding the max of historgram on y axis)? Is it more clear?
Yes, example:
# ignore the first plot:
truehist(b)
yl <- par("usr")[4]
d <- density(b[20:50])
truehist(b, ylim=c(0, max(yl, d$y)))
lines(d)
Uwe Ligges
Best, --- On Tue, 4/28/09, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote: From: Uwe Ligges <ligges at statistik.tu-dortmund.de> Subject: Re: [R] truehist and density plots To: "carol white" <wht_crl at yahoo.com> Cc: r-help at stat.math.ethz.ch Date: Tuesday, April 28, 2009, 5:42 AM carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density
function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows:
length(b) = 1000 truehist(b) lines(density(b[1:100]))
I do not undertsand what you mean. Can you please provide a *reproducible* example? Uwe Ligges
however the density plot of the first 100 points exceeds the max of y axis
(see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set?
Cheers,
Carol
------------------------------------------------------------------------
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090428/05ce674f/attachment-0001.pl>
carol white wrote:
OK but how to generalize so that the max of y would cover for all subsets of b? because 50:70 was just an example, so consider other subsets (known or unknow in advance) whose density will be plotted on the histogram.
Take all subsets at first and calculate the max of all those density estimates of all substes, then plot all the estimates, as I have done for just 1 sample.
But since b is the full set, why it doesn't contain the max of y for all subsets?
Consult a textbook about statistics: Consider a density estimate of just very few numbers with very small variance (which might happen), you'd get a very high estimate at the center. Uwe Ligges
thanks --- On Tue, 4/28/09, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote: From: Uwe Ligges <ligges at statistik.tu-dortmund.de> Subject: Re: [R] truehist and density plots To: "carol white" <wht_crl at yahoo.com> Cc: r-help at stat.math.ethz.ch Date: Tuesday, April 28, 2009, 6:16 AM carol white wrote:
Consider a vector of 100 elements (attached files). then, truehist(b) lines(density(b[20:50])) How is it possible to have density plots of all subsets like b[20:50]
within histogram (without exceeding the max of historgram on y axis)?
Is it more clear?
Yes, example:
# ignore the first plot:
truehist(b)
yl <- par("usr")[4]
d <- density(b[20:50])
truehist(b, ylim=c(0, max(yl, d$y)))
lines(d)
Uwe Ligges
Best, --- On Tue, 4/28/09, Uwe Ligges <ligges at statistik.tu-dortmund.de>
wrote:
From: Uwe Ligges <ligges at statistik.tu-dortmund.de> Subject: Re: [R] truehist and density plots To: "carol white" <wht_crl at yahoo.com> Cc: r-help at stat.math.ethz.ch Date: Tuesday, April 28, 2009, 5:42 AM carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density
function of subsets of the vector on the histogram. So I use truehist in
MASS
package and lines(density) as follows:
length(b) = 1000 truehist(b) lines(density(b[1:100]))
I do not undertsand what you mean. Can you please provide a *reproducible* example? Uwe Ligges
however the density plot of the first 100 points exceeds the max of y
axis
(see attached). how is it possible to make a graphics so that the density
plot
of the subsets doesn't go beyond the maximum of all points in the
complete
set?
Cheers,
Carol
------------------------------------------------------------------------
______________________________________________ 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.
Perhaps like this : temp =density(b[20:50]) truehist(b, ymax=max(temp$y)) lines(temp) Etienne carol white a ?crit :
Consider a vector of 100 elements (attached files). then, truehist(b) lines(density(b[20:50])) How is it possible to have density plots of all subsets like b[20:50] within histogram (without exceeding the max of historgram on y axis)? Is it more clear? Best, --- On Tue, 4/28/09, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote: From: Uwe Ligges <ligges at statistik.tu-dortmund.de> Subject: Re: [R] truehist and density plots To: "carol white" <wht_crl at yahoo.com> Cc: r-help at stat.math.ethz.ch Date: Tuesday, April 28, 2009, 5:42 AM carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density
function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows:
length(b) = 1000 truehist(b) lines(density(b[1:100]))
I do not undertsand what you mean. Can you please provide a *reproducible* example? Uwe Ligges
however the density plot of the first 100 points exceeds the max of y axis
(see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set?
Cheers,
Carol
------------------------------------------------------------------------
______________________________________________ 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. ------------------------------------------------------------------------ ______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090428/b42a957b/attachment-0001.pl>
carol white wrote:
Hi, I wanted to plot the histogram of a vector and then, plot the density function of subsets of the vector on the histogram. So I use truehist in MASS package and lines(density) as follows: length(b) = 1000 truehist(b) lines(density(b[1:100])) however the density plot of the first 100 points exceeds the max of y axis (see attached). how is it possible to make a graphics so that the density plot of the subsets doesn't go beyond the maximum of all points in the complete set?
Hi Carol, You can use the rescale function in the plotrix package to do things like this. Have a look at the example on the help page: # scale one vector into the range of another normal.counts<-rnorm(100) normal.tab<-tabulate(cut(normal.counts,breaks=seq(-3,3,by=1))) normal.density<-rescale(dnorm(seq(-3,3,length=100)),range(normal.tab)) # now plot them plot(c(-2.5,-1.5,-0.5,0.5,1.5,2.5),normal.tab,xlab="X values", type="h",col="green") lines(seq(-3,3,length=100),normal.density,col="blue") Jim