如何将tableGrobs添加到动态生成的ggplot中 - R

时间:2015-11-25 01:40:03

标签: r ggplot2 gridextra

我有代码在一组数据工作中生成一组直方图,并且我已经获得了为每个直方图工作生成一组汇总表的代码,但我无法结合直方图和表格。

在示例案例中使用iris数据:

#Generate list of data to be create ggplot histogram
iris.hp<-ggplot(data=iris, aes(x=Sepal.Length)) +
   geom_histogram(binwidth =.25,origin=-0.125,
        right = TRUE,col="white", fill="steelblue4",alpha=1) + 
   labs(title = "Iris Sepal Length")+
   labs(x="Sepal Length", y="Count")
iris.hp
iris.list<-by(data = iris, INDICES = iris$Species, simplify = TRUE, 
    FUN  = function(x) {iris.hp %+% x + ggtitle(unique(x$Species))})
multi.plot<-marrangeGrob(grobs = iris.list, nrow=1,ncol=1, 
    top = quote(paste(iris$labels$title,'\nPage',g,'of',pages)))


#Generate list of data to create summary statistics table
sum.str<-aggregate(Sepal.Length~Species,iris,summary)
spec<-sum.str[,1]
spec.stats<-sum.str[,2]
sum.data<-data.frame(spec,spec.stats)
sum.table<-tableGrob(sum.data.frame)
colnames(sum.data)<-c("species","sep.len.min","sep.len.1stQ","sep.len.med",
    "sep.len.mean","sep.len.3rdQ","sep.len.max")
table.list<-by(data = sum.data, INDICES = sum.data$"species", 
     simplify = TRUE, FUN = function(x) {tableGrob(x,theme=tt3)})
multi.plot.table<-marrangeGrob(grobs = table.list,nrow=1,ncol=1, 
     top = quote(paste(iris$labels$Species,'\nPage', g, 'of',pages)))

#attempt to combine the iris.list and table.list Grobs
# updated code based on @Heroka commment 
multi.plot.test<-marrangeGrob(grobs=c(iris.list,table.list),
    nrow=1,ncol=2, top = quote(paste(occ$labels$title,'\nPage', g, 'of',pages)))

我可以使用annotation_customgrid.arrange + arrangeGrob为单个实例执行此操作,我尝试使用marrangeGrob函数,但没有运气。只需将iris.listtable.list同时放入marrangeGrob就会引发错误: Error in gList(list(setosa = list(data = list(Sepal.Length = c(5.1, 4.9, : only 'grobs' allowed in "gList" 更新:更改marrangeGrob(grobs = list() to grobs = c()时出现错误,感谢@Heroka

任何人都有关于如何组合iris.list和table.list grobs的指针,并以直方图匹配相应的摘要统计表的方式对它们进行排序?我尝试使用gList进行组合,但它在gList中返回了一个错误'only grobs',而且我也使用gTree进行了调整,但无济于事。

1 个答案:

答案 0 :(得分:0)

好吧,我终于明白了,看起来很简单。为了将两组grob(虹膜直方图iris.list和摘要统计表table.list)组合/交错为glist可用的单marrangeGrob,您可以使用

 marrangeGrob(grobs=(c(rbind(iris.list,table.list))) 

最终结果是每种类型虹膜的单独直方图和汇总表:setosa,verginica和versicolor。

更新后的工作代码如下。

#Generate list of data to be create ggplot histogram
 iris.hp<-ggplot(data=iris, aes(x=Sepal.Length)) +
  geom_histogram(binwidth =.25,origin=-0.125,
    right = TRUE,col="white", fill="steelblue4",alpha=1) + 
      labs(title = "Iris Sepal Length")+
      labs(x="Sepal Length", y="Count")
#Plots histogram of full iris dataset
 iris.hp
#Creates list of histogram plots for each iris using the base{by} function 
 iris.list<-by(data = iris, INDICES = iris$Species, simplify = TRUE, 
    FUN = function(x) {iris.hp %+% x + ggtitle(unique(x$Species))})
#Outputs a plot for each iris histogram
 multi.plot<-marrangeGrob(grobs = iris.list, nrow=1,ncol=1, 
     top = quote(paste(iris$labels$title,'\nPage', g, 'of',pages)))

#Generate list of data to create summary statistics table
 sum.str<-aggregate(Sepal.Length~Species,iris,summary)
 spec<-sum.str[,1]
 spec.stats<-sum.str[,2]
 sum.data<-data.frame(spec,spec.stats)
 sum.table<-tableGrob(sum.data)
 colnames(sum.data)<-c("species","sep.len.min","sep.len.1stQ","sep.len.med",
    "sep.len.mean","sep.len.3rdQ","sep.len.max")
#Creates list of summary table grobs for each iris
 table.list<-by(data = sum.data, INDICES = sum.data$"species", simplify = TRUE, 
     FUN = function(x) {tableGrob(x,theme=tt3)})
#Outputs multiple summary tables for each iris
 multi.plot.table<-marrangeGrob(grobs = table.list,nrow=1,ncol=1, 
     top = quote(paste(iris$labels$Species,'\nPage', g, 'of',pages)))


#Combined histogram and summary table across multiple plots
 multi.plots<-marrangeGrob(grobs=(c(rbind(iris.list,table.list))),nrow=2, ncol=1, 
     top = quote(paste(occ$labels$title,'\nPage', g, 'of',pages)))