Skip to content
Back to formatted view

Raw Message

Message-ID: <49F80531.3000403@bitwrit.com.au>
Date: 2009-04-29T07:43:45Z
From: Jim Lemon
Subject: truehist and density plots
In-Reply-To: <470484.67084.qm@web62002.mail.re1.yahoo.com>

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