2015-11-25 23 views
1

我已经得到了用于生成一组数据的一组直方图的代码,并且我已经获得了用于为每个直方图生成一组汇总表的代码工作,但我还没有能够结合直方图和表格。在例如情况下如何将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.listmarrangeGrob引发错误: Error in gList(list(setosa = list(data = list(Sepal.Length = c(5.1, 4.9, : only 'grobs' allowed in "gList" UPDATE:错误就解决了改变marrangeGrob(grobs = list() to grobs = c()感谢时@Heroka

任何一个有关于如何将iris.list和table.list grobs结合任何指针并按照直方图与相应的摘要统计表匹配的顺序排列它们?我试图结合使用gList,但它返回了gList中允许的错误'only grobs',并且我也使用gTree无用。

+0

你可以用它代替名单'C'在“grobs =”,但我认为你需要做的grobs一些重新排序因为它们被放置ONT他在序页面。 – Heroka

+0

感谢hep @ Heloka。更改为'grobs = c(iris.list,table.list)'确实一次绘制了所有内容,但是你是对的,它并没有将正确的表格添加到正确的历史图表中。任何关于如何组合和排序两个grobs以产生正确序列的指针? – AKP

回答

0

嗯,我终于明白了,这似乎令人尴尬地简单。为了结合/交织两组grobs的(虹膜直方图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))) 
相关问题