Isaac
Isaac

Reputation: 1536

Plot all columns from a data.frame in a subplot with ggplot2

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.

Upvotes: 0

Views: 697

Answers (1)

Robin Gertenbach
Robin Gertenbach

Reputation: 10776

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")

Upvotes: 2

Related Questions