0

I'm using this function to get some exploratory analysis in r

nums <- sapply(db, is.numeric)
sapply(db[,nums], function(x) {
  nome <- as.character(names(x))
  hist(x)
  lines(density(x, na.rm = T))

  })

How can i print the name of the column in the plot as x axis? I've tried with apply over matrix by column - any way to get column name? but I can't figure out the second part of the function to make it work in this case

Community
  • 1
  • 1
GGA
  • 385
  • 5
  • 22
  • 1
    Can you provide example data for db? – JohnSG Apr 15 '16 at 13:53
  • it's a database with both numeric and factors, so the function which assigns to nums will return a vector with TRUE if numeric and FALSE with factor columns respectively, i'll try to give some data sample – GGA Apr 15 '16 at 13:57
  • Paste output of `dput` function with a head of your data. – m-dz Apr 15 '16 at 13:59
  • I currently can't access the dataset, the iris dataset would be the same since it has both numeric and non numeric variables – GGA Apr 15 '16 at 14:06

2 Answers2

0

One way would be to loop over the column names:

nums <- sapply(db, is.numeric)
numsNames <- names(db)[nums]

sapply(numsNames, function(x) {
  hist(db[,x], xlab=x)
  lines(density(db[,x], na.rm = T))
})
lmo
  • 37,904
  • 9
  • 56
  • 69
0

You could use mapply to take the data and other arguments to hist

num <- sapply(iris, is.numeric)
opar <- par(mfrow = c(2,2))
histDensity <- function(x, ...){
  hist(x = x, freq = FALSE, ...)
  lines(density(x, na.rm = TRUE))
}
mapply(histDensity, x = iris[, num], main = names(iris[, num]))
par(opar)

Alternatively, you could use ggplot

library(reshape2)
library(ggplot2)
iris2 <- melt(iris)
ggplot(iris2, aes(x = value, y = ..density..)) + 
  geom_histogram() +
  geom_density() +
  facet_wrap(~variable)
Richard Telford
  • 9,558
  • 6
  • 38
  • 51