How can I create a histogram for all variables in a data set with minimal effort in R?

北慕城南 提交于 2019-11-30 19:50:26

There may be three broad approaches:

  1. Commands from packages such as hist.data.frame()
  2. Looping over variables or similar macro constructs
  3. Stacking variables and using facets

Packages

Other commands available that may be helpful:

library(plyr)
library(psych)
multi.hist(mpg) #error, not numeric
multi.hist(mpg[,sapply(mpg, is.numeric)])

or perhaps multhist from plotrix, which I haven't explored. Both of them do not offer the flexibilty I was looking for.

Loops

As an R beginner everyone advised me to stay away from loops. So I did, but perhaps it is worth a try here. Any suggestions are very welcome. Perhaps you could comment on how to combine the graphs into one file.

Stacking

My first suspicion was that stacking variables might get out of hand. However, it might be the best strategy for a reasonable set of variables.

One example I came up with uses the melt function.

library(reshape2)
mpgid <- mutate(mpg, id=as.numeric(rownames(mpg)))
mpgstack <- melt(mpgid, id="id")
pp <- qplot(value, data=mpgstack) + facet_wrap(~variable, scales="free")
# pp + stat_bin(geom="text", aes(label=..count.., vjust=-1))
ggsave("mpg-histograms.pdf", pp, scale=2)

(As you can see I tried to put value labels on the bars for more information density, but that didn't go so well. The labels on the x-axis are also less than ideal.)

No solution here is perfect and there won't be a one-size-fits-all command. But perhaps we can get closer to ease exploring a new data set.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!