Re: [R] adding lines to multiple plot

From: Ben Bolker <>
Date: Wed, 30 Jul 2008 14:04:09 +0000 (UTC)

Gary W <g.watmough <at>> writes:

> Hi
> I am trying to add density lines to multiple histograms created using R. I
> have managed to get all 18 lines on the last histogram in the series,
> however, i cannot figure out how to get each line appear on the correct
> histogram plot. I know in Matlab there is a hold function, is there
> something similar in R?
> ##find unique column names in table
> variablenames<-unique(names(variables))
> ## define number of unique names
> Nvariables<-length(variablenames)
> for(i in 1:Nvariables){
> par(mfrow=c(1,1),ask=TRUE)
> }

  previous loop is a little weird -- it has exactly the same result as just running the command inside once.

Labeling with the numeric values is a bit of a pain. If you don't need them you can just do


   function(v) {



If you need the labels you can either do

mapply(function(v,i) {

   hist(v,prob=TRUE,main=paste("Histogram of",i),xlab=i)


Or it might be easier to just do the whole thing with a for loop:

for (i in 1:Nvariables) {
  hist(variables[[i]],prob=TRUE,main=paste("Histogram of",i),xlab=i)

      lines(density(variables[[i]]),lwd=2) }

  Ben Bolker mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 30 Jul 2008 - 14:06:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Jul 2008 - 15:03:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive