Hi, Just a quick query - if I'm creating a function to produce a number of histograms per page of output (one per column from a matrix), how can I pass the column name of the matrix into the title (or indeed to form part of the x-axis label)? TIA, Laura Laura Quinn Institute of Atmospheric Science School of Earth and Environment University of Leeds Leeds LS2 9JT tel: +44 113 343 1596 fax: +44 113 343 6716 mail: laura at env.leeds.ac.uk
Laura Quinn wrote:> Hi, > > Just a quick query - if I'm creating a function to produce a number of > histograms per page of output (one per column from a matrix), how can I > pass the column name of the matrix into the title (or indeed to form part > of the x-axis label)?By extracting them using colnames()? Uwe Ligges> TIA, > Laura > > Laura Quinn > Institute of Atmospheric Science > School of Earth and Environment > University of Leeds > Leeds > LS2 9JT > > tel: +44 113 343 1596 > fax: +44 113 343 6716 > mail: laura at env.leeds.ac.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
a simple thing to do is: mat <- matrix(...) # your matrix nams <- dimnames(mat)[[2]] for(j in 1:ncol(mat)) hist(mat[,j], main=nams[j]) # or hist(mat[,j], xlab=paste("...", nams[j], "...")) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Laura Quinn" <laura at env.leeds.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, February 16, 2005 2:56 PM Subject: [R] Passing colnames to graphics title> Hi, > > Just a quick query - if I'm creating a function to produce a number > of > histograms per page of output (one per column from a matrix), how > can I > pass the column name of the matrix into the title (or indeed to form > part > of the x-axis label)? > > TIA, > Laura > > Laura Quinn > Institute of Atmospheric Science > School of Earth and Environment > University of Leeds > Leeds > LS2 9JT > > tel: +44 113 343 1596 > fax: +44 113 343 6716 > mail: laura at env.leeds.ac.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
If i is 1:20, there are no column names. Make sure you are indexing the names from your your dataframe.> xx <- data.frame(a=c(1:10), b = letters[1:10]) > colnames(xx)[1] "a" "b"> for(i in 1:2) print(colnames(xx)[i])[1] "a" [1] "b"> for(i in colnames(xx)) print(i)[1] "a" [1] "b" Matt Austin Statistician Amgen One Amgen Center Drive M/S 24-2-C Thousand Oaks CA 93021 (805) 447 - 7431> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Laura Quinn > Sent: Wednesday, February 16, 2005 6:47 AM > To: Uwe Ligges > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Passing colnames to graphics title > > > Obviously I have been trying to use the colnames() function! > > However, when I try to subscript ie: > > for(i in 1:20){ > main=paste("Site:",colnames(i),sep="") > } > > this doesn't work! I thought that as.character(colnames(i)) or > substitute(colnames(i)) might work, but to no avail... > > Laura Quinn > Institute of Atmospheric Science > School of Earth and Environment > University of Leeds > Leeds > LS2 9JT > > tel: +44 113 343 1596 > fax: +44 113 343 6716 > mail: laura at env.leeds.ac.uk > > On Wed, 16 Feb 2005, Uwe Ligges wrote: > > > Laura Quinn wrote: > > > > > Hi, > > > > > > Just a quick query - if I'm creating a function to > produce a number of > > > histograms per page of output (one per column from a > matrix), how can I > > > pass the column name of the matrix into the title (or > indeed to form part > > > of the x-axis label)? > > > > > > By extracting them using colnames()? > > > > Uwe Ligges > > > > > > > > > TIA, > > > Laura > > > > > > Laura Quinn > > > Institute of Atmospheric Science > > > School of Earth and Environment > > > University of Leeds > > > Leeds > > > LS2 9JT > > > > > > tel: +44 113 343 1596 > > > fax: +44 113 343 6716 > > > mail: laura at env.leeds.ac.uk > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Either set the 'main' or 'xlab' in the hist(). See help("par") for more information on graphical arguments or help("hist"). mat <- matrix( rnorm(1000), nc=5 ) colnames(mat) <- LETTERS[1:ncol(mat)] for( i in 1:ncol(mat) ){ hist( mat[ ,i], main=paste( "Histogram of data from column ", colnames(mat)[i] ), xlab="" ) } On Wed, 2005-02-16 at 13:56 +0000, Laura Quinn wrote:> Hi, > > Just a quick query - if I'm creating a function to produce a number of > histograms per page of output (one per column from a matrix), how can I > pass the column name of the matrix into the title (or indeed to form part > of the x-axis label)? > > TIA, > Laura > > Laura Quinn > Institute of Atmospheric Science > School of Earth and Environment > University of Leeds > Leeds > LS2 9JT > > tel: +44 113 343 1596 > fax: +44 113 343 6716 > mail: laura at env.leeds.ac.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >