almo
almo

Reputation: 561

How to add column names to histogram when using par() function in r

I would like to plot all histogram in my data frame. One way I tried was hist.data.frame(df) which gave me very small pictures of each. Then I tried this code:

library(datasets)
data(iris)
X<-iris[,0:3]
par(mfrow=c(2,1))
histout=apply(X,2,hist)

This time, each picture is big enough but all of them have a title like Histogram NewX[,i]. When I have so many variables, this is very unclear. Is there anyway that I can add column name to each graph? Thank you!

Upvotes: 0

Views: 579

Answers (1)

Vincent
Vincent

Reputation: 17715

I recommend you use lapply and feed extra arguments to the hist function in there (main, xlab, etc.). You could also use a loop.

For instance,

library(datasets)
data(iris)
X<-iris[,0:3]
par(mfrow=c(3,1))
lapply(names(X), function(k) hist(X[[k]], main=k))

Edit: Sorry, this is essentially the same answer as given in comments. I had not seen it.

Upvotes: 3

Related Questions