简体   繁体   English

使用ggplot2在子图中绘制data.frame中的所有列

[英]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. 如标题所示,我想绘制data.frame中的所有列,但是我想以一种通用的方式来做。 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)/ 多重绘图功能来自哪里: 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 我的数据集来自Kaggle(房屋价格预测): 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. 编辑:使用gridExtra作为@LAP建议也不会给我带来很好的结果。 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. 同样,SaleCondition是唯一打印出来的东西,效果不是很好。 PD: I also tried cowplot, same result. PD:我也尝试过Cowplot,同样的结果。

Using tidyr you can do something like the following: 使用tidyr,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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