简体   繁体   中英

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. 输出量

This would be the last column well represented: 在此处输入图片说明

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: 在此处输入图片说明 Again, SaleCondition is the only thing printed and not very well. PD: I also tried cowplot, same result.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM