[R] Histogram + % of cases for a given criteria

Jim Lemon jim at bitwrit.com.au
Thu May 14 14:04:33 CEST 2009


S. Nunes wrote:
> Hi all,
>
> I am doing some explorations using a dataset with the following
> structure (id, value, flag).
> For instance:
>
> a, 2.2, 1
> b, 3.0, 1
> c, 2.9, 0
> d, 3.1, 1
> ...
>
> I have plotted a standard histogram using a simple command like:
> hist(data$value)
>
> My question:
>
> I would like to superimpose a line ([0%-100%] scale) representing the
> % of values that, for each class of the histogram, have the $flag
> equal to 1.
>
>
> What strategy
>
> I hope I made myself clear. Please let me know if not.
>
>   
Hi Sergio,
A curly one indeed. Try this:

crit_cuts<-function(x,breaks,right=TRUE,crit_var,crit) {
 ncats<-length(breaks)-1
 critcount<-rep(0,ncats)
 for(i in 1:ncats) {
  if(right) inside<-x >= breaks[i] & x < breaks[i+1]
  else inside<-x > breaks[i] & x <= breaks[i+1]
  critcount[i]<-sum(crit_var[inside]==crit)
 }
 return(critcount)
}
x<-rnorm(30)+5
crit<-sample(0:1,30,TRUE)
histinfo<-hist(x)
critcount<-crit_cuts(x,histinfo$breaks,crit_var=crit,crit=1)
segments(histinfo$breaks[1:(length(histinfo$breaks)-1)],
  critcount,histinfo$breaks[-1],critcount,lty=2,col="red")

Jim




More information about the R-help mailing list