2015-11-25 66 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))) 
相關問題