2016-04-15 58 views
0

我使用这个函数来得到R中的一些探索性分析[R sapply命名,以图X轴

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

    }) 

我怎么能在剧情为x轴打印列的名称? 我试着apply over matrix by column - any way to get column name?但我不能找出函数的第二部分,使其在这种情况下工作

+1

你能提供db的示例数据吗? – JohnSG

+0

它是一个既有数字又有因子的数据库,所以赋予nums的函数将分别返回一个带有TRUE的向量,如果是numeric和带有因子列的FALSE,我会尝试给出一些数据示例 – GGA

+0

粘贴'dput'函数的输出您的数据的头。 –

回答

0

一种方法是遍历列名:

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)) 
}) 
0

你可以使用mapply取数据和其他参数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) 

或者,你可以使用ggplot

library(reshape2) 
library(ggplot2) 
iris2 <- melt(iris) 
ggplot(iris2, aes(x = value, y = ..density..)) + 
    geom_histogram() + 
    geom_density() + 
    facet_wrap(~variable)