Re: [R] Passing colnames to graphics title
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 +, 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: [EMAIL PROTECTED] > > __ > R-help@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@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
RE: [R] Passing colnames to graphics title
Dear Laura, It doesn't make sense to call colnames() with the loop index; you could do something like (for the matrix or data frame X): par(mfrow=c(1, ncol(X))) names <- colnames(X) for (i in seq(along=names)) hist(X[,i], main="", xlab=paste("Site:", names[i])) I hope this helps, John John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Laura Quinn > Sent: Wednesday, February 16, 2005 9:47 AM > To: Uwe Ligges > Cc: r-help@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: [EMAIL PROTECTED] > > 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: [EMAIL PROTECTED] > > > > > > __ > > > R-help@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@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@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
RE: [R] Passing colnames to graphics title
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: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of Laura Quinn > Sent: Wednesday, February 16, 2005 6:47 AM > To: Uwe Ligges > Cc: r-help@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: [EMAIL PROTECTED] > > 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: [EMAIL PROTECTED] > > > > > > __ > > > R-help@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@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@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
Re: [R] Passing colnames to graphics title
On Wed, Feb 16, 2005 at 02:46:51PM +, Laura Quinn wrote: > 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="") ^^^ it looks to me that this should be something like colnames(foo)[i] where foo is the matrix or data.frame you use. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@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
Re: [R] Passing colnames to graphics title
Laura Quinn wrote: 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, You should (re)read ?colnames. It takes a matrix as it's argument, not an integer as you have supplied. I think you want: for(i in 1:20){ main=paste("Site:",colnames(mat)[i],sep="") } --sundar 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: [EMAIL PROTECTED] 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: [EMAIL PROTECTED] __ R-help@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@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@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
Re: [R] Passing colnames to graphics title
Uwe Ligges wrote: Laura Quinn wrote: 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="") } BTW: colnames(i) is the same as colnames(1) in the first iteration of your loop. What do you expect colnames(1) to be? Uwe Ligges Example (which you should have provided): # Generate an example-matrix: X <- matrix(1:9, 3) colnames(X) <- letters[1:3] # now try to get histograms of columns using a loop: par(mfrow = c(3, 1)) cnames <- colnames(X) for(i in 1:ncol(X)){ hist(X[,i], main = paste("Site:", cnames[i], sep="")) } Uwe Ligges 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: [EMAIL PROTECTED] 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: [EMAIL PROTECTED] __ R-help@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@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
Re: [R] Passing colnames to graphics title
Laura Quinn wrote: 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="") } Example (which you should have provided): # Generate an example-matrix: X <- matrix(1:9, 3) colnames(X) <- letters[1:3] # now try to get histograms of columns using a loop: par(mfrow = c(3, 1)) cnames <- colnames(X) for(i in 1:ncol(X)){ hist(X[,i], main = paste("Site:", cnames[i], sep="")) } Uwe Ligges 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: [EMAIL PROTECTED] 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: [EMAIL PROTECTED] __ R-help@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@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
Re: [R] Passing colnames to graphics title
Wonderful, thank you very much! 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: [EMAIL PROTECTED] On Wed, 16 Feb 2005, Dimitris Rizopoulos wrote: > 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" <[EMAIL PROTECTED]> > To: > 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: [EMAIL PROTECTED] > > > > __ > > R-help@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@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
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: [EMAIL PROTECTED] 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: [EMAIL PROTECTED] > > > > __ > > R-help@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@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
Re: [R] Passing colnames to graphics title
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" <[EMAIL PROTECTED]> To: 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: [EMAIL PROTECTED] __ R-help@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@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
Re: [R] Passing colnames to graphics title
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: [EMAIL PROTECTED] __ R-help@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@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