Uli Kleinwechter
2008-Mar-17 10:47 UTC
[R] Creating plots for all variables in a data frame and printing them with the variable name in the main title
Dear all, I'm just trying to create plots for all variables in a dataframe (named "x") using the following: png() apply(x,2,hist) Just as intended, it produces one plot for each variable. Unfortunately, the main title of each graph is "Histogram of newX[,i]" instead of "Histogram of name of variable". This makes it impossible to assign the graphs to the variables. Is there a way to change this and to make R use the correct variable names in the title of the plot? Thank you very much, Uli
Henrique Dallazuanna
2008-Mar-17 11:16 UTC
[R] Creating plots for all variables in a data frame and printing them with the variable name in the main title
Try: x <- data.frame(A=rnorm(100), B=rnorm(100), C=runif(100)) sapply(names(x), function(i)hist(x[,i], main = i)) On 17/03/2008, Uli Kleinwechter <ulikleinwechter at yahoo.com.mx> wrote:> Dear all, > > I'm just trying to create plots for all variables in a dataframe (named > "x") using the following: > > png() > apply(x,2,hist) > > Just as intended, it produces one plot for each variable. Unfortunately, > the main title of each graph is "Histogram of newX[,i]" instead of > "Histogram of name of variable". This makes it impossible to assign the > graphs to the variables. Is there a way to change this and to make R use > the correct variable names in the title of the plot? > > Thank you very much, > > Uli > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Prof Brian Ripley
2008-Mar-17 11:35 UTC
[R] Creating plots for all variables in a data frame and printing them with the variable name in the main title
On Mon, 17 Mar 2008, Uli Kleinwechter wrote:> Dear all, > > I'm just trying to create plots for all variables in a dataframe (named > "x") using the following: > > png() > apply(x,2,hist)Please don't use apply() on a data frame: you want lapply(x, hist) here.> Just as intended, it produces one plot for each variable. Unfortunately, > the main title of each graph is "Histogram of newX[,i]" instead of > "Histogram of name of variable". This makes it impossible to assign the > graphs to the variables. Is there a way to change this and to make R use > the correct variable names in the title of the plot?You need to tell hist() what the title is to be. I'd just use a loop, e.g. for(xn in names(x)) hist(x[[xn]], title = paste("Histogram of", xn)) but you could use lapply by e.g. lapply(names(x), function(xn) hist(x[[xn]], title = paste("Histogram of", xn))) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
bartjoosen
2008-Mar-17 15:04 UTC
[R] Creating plots for all variables in a data frame and printing them with the variable name in the main title
Here is one way: lapply(1:ncol(x),function(i) hist(x[,i], main =paste("Histogram of",names(x)[i]))) Bart Uli Kleinwechter wrote:> > Dear all, > > I'm just trying to create plots for all variables in a dataframe (named > "x") using the following: > > png() > apply(x,2,hist) > > Just as intended, it produces one plot for each variable. Unfortunately, > the main title of each graph is "Histogram of newX[,i]" instead of > "Histogram of name of variable". This makes it impossible to assign the > graphs to the variables. Is there a way to change this and to make R use > the correct variable names in the title of the plot? > > Thank you very much, > > Uli > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Creating-plots-for-all-variables-in-a-data-frame-and-printing-them-with-the-variable-name-in-the-main-title-tp16092442p16093308.html Sent from the R help mailing list archive at Nabble.com.