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



我有代码在一组工作的数据中生成一组直方图,我有代码为每个工作的直方图生成一组汇总表,但我无法将直方图和表组合起来。

在示例案例中使用虹膜数据:

#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_custom为单个实例执行此操作,grid.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"更新:由于@Heroka ,更改marrangeGrob(grobs = list() to grobs = c()时解决了错误

任何人都有关于如何组合iris.list和table.list grobs的指针,并以直方图与适当的汇总统计表匹配的方式对它们进行排序?我试图使用gList进行组合,但它返回了一个错误"gList中只允许使用grobs",我还用gTree四处寻找,但没有成功。

好吧,我终于弄明白了,它看起来简单得令人尴尬。为了将两组Grob(虹膜直方图iris.list和汇总统计表table.list)组合/交错为marrangeGrob可用的单个glist,可以使用

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

最新更新