1

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).

For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.

After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?

I appreciate any help!

histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,  
      type='density',
      nint=100,
      breaks=NULL,
      layout=c(10,3),  
      scales= list(y=list(relation="free"),
                   x=list(relation="free")),
      panel=function(x,...) {
                             mean.values <-mean(x)
                             sample.n <- length(x)
                             panel.text(lab=paste("Sample size = ",sample.n))
                             panel.text(lab=paste("Mean = ",mean.values))
                             panel.histogram(x,col="pink", ...) 
                             panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
2
  • 2
    Doesn't panel.text need an 'x' (and a 'y' argument) to tell the interpreter where to put the label? Commented Jul 9, 2013 at 22:15
  • @DWin Thanks! I'll work on that. Commented Jul 9, 2013 at 23:30

1 Answer 1

0

A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:

https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.