我已经得到了用于生成一组数据的一组直方图的代码,并且我已经获得了用于为每个直方图生成一组汇总表的代码工作,但我还没有能够结合直方图和表格。在例如情况下如何将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.list
和table.list
到marrangeGrob
引发错误: 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无用。
你可以用它代替名单'C'在“grobs =”,但我认为你需要做的grobs一些重新排序因为它们被放置ONT他在序页面。 – Heroka
感谢hep @ Heloka。更改为'grobs = c(iris.list,table.list)'确实一次绘制了所有内容,但是你是对的,它并没有将正确的表格添加到正确的历史图表中。任何关于如何组合和排序两个grobs以产生正确序列的指针? – AKP