Here is a (not so elegant) solution using lattice
, where I consider quartiles in case the variables have numeric or integer values. Note that I assume that your classification factor is always in the latest position in your dataframe.
mydata <- data.frame(age=rnorm(100, 25, 4),
sugar=sample(0:10, 100, rep=T),
spinach=sample(c("true","false"), 100, rep=T),
meat=sample(c("true","false"), 100, rep=T),
milk=sample(c("true","false"), 100, rep=T),
class=sample(c("true","false"), 100, rep=T))
library(lattice)
library(gridExtra)
library(Hmisc)
plt <- list()
for (i in 1:(ncol(mydata)-1)) {
if (is.numeric(mydata[,i])) vv <- cut2(mydata[,i], g=4)
else vv <- mydata[,i]
plt[[i]] <- barchart(xtabs(~ vv + mydata[,"class"]), horizontal=F,
main=colnames(mydata)[i],
col=c("red","blue"), xlab="", ylab="", box.width=1,
lattice.options=list(axis.padding=list(factor=0.5)),
scales=list(x=list(rot=ifelse(is.numeric(mydata[,i]),45,0))))
}
plt[[i+1]] <- barchart(xtabs(~ class, mydata), col=c("red","blue"),
xlab="", ylab="", box.width=1,
lattice.options=list(axis.padding=list(factor=0.5)),
horizontal=F, main="class")
do.call(grid.arrange, plt)
Using the same dataset with 10 more variables
mydata <- data.frame(age=rnorm(100, 25, 4),
sugar=sample(0:10, 100, rep=T),
spinach=sample(c("true","false"), 100, rep=T),
meat=sample(c("true","false"), 100, rep=T),
milk=sample(c("true","false"), 100, rep=T),
replicate(10, sample(c("true","false"), 100, rep=T)),
class=sample(c("true","false"), 100, rep=T))
this is the base
version (you will still need the Hmisc
library):
opar <- par(mfrow=c(4,4))
for (i in 1:15) {
if (is.numeric(mydata[,i])) vv <- cut2(mydata[,i], g=4)
else vv <- mydata[,i]
barplot(xtabs(~ mydata[,"class"] + vv), col=c("red","blue"),
main=colnames(mydata)[i],
las=ifelse(is.numeric(mydata[,i]), 2, 1))
}
barplot(xtabs(~ class, mydata), col=c("red","blue"), main="class",
las=ifelse(is.numeric(mydata[,i]), 2, 1))
par(opar)
age
, becausesugar
will actually be treated as a factor): How would like to summarize it with other factors (quartiles, median, mean)? $\endgroup$