0
votes

as the title suggest, I want to plot all columns from my data.frame, but I want to do it in a generic way. All my columns are factor. Here is my code so far:

nums <- sapply(train_dataset, is.factor) #Select factor columns
factor_columns <- train_dataset[ , nums]

plotList <- list()
for (i in c(1:NCOL(factor_columns))){
  name = names(factor_columns)[i]
  p <- ggplot(data = factor_columns) +  geom_bar(mapping = aes(x = name))
  plotList[[i]] <- p
}
multiplot(plotList, cols = 3)

where multiplot function came from here: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

And my dataset came from Kaggle (house pricing prediction): https://www.kaggle.com/c/house-prices-advanced-regression-techniques

What I get from my code is the image below, which appears to be the last column badly represented. Output

This would be the last column well represented: enter image description here

EDIT: Using gridExtra as @LAP suggest also doesn't give me a good result. I use this instead of multiplot.

nCol <- floor(sqrt(length(plotList)))
do.call("grid.arrange", c(plotList, ncol=nCol))

but what I get is this: enter image description here Again, SaleCondition is the only thing printed and not very well. PD: I also tried cowplot, same result.

1
I would add additional SaleCondition column to original dataset and plot everything in one graph.pogibas
The package gridExtra might also help.LAP
Hi @PoGibas SaleCondition is already in the dataset, what I mean is that when I try to do subplot my data, all I have is the last column which is 'SaleCondition`. I cannot plot it all in one graph beacuse I'm want to plot bars. @LAP I'll take a look at that.Isaac

1 Answers

2
votes

Using tidyr you can do something like the following:

factor_columns %>% 
  gather(factor, level) %>%
  ggplot(aes(level)) + geom_bar() + facet_wrap(~factor, scales = "free_x")