如何将 tableGrobs 添加到动态生成的 ggplot - R
How to add tableGrobs to dynamically generated ggplot - R
我有代码可以在一组工作数据中生成一组直方图,我有代码可以为每个工作直方图生成一组摘要 tables,但是我无法将直方图和 table 组合起来。
在示例中使用虹膜数据:
#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"
更新:更改时错误已解决 marrangeGrob(grobs = list() to grobs = c()
感谢@Heroka
任何人都知道如何组合 iris.list 和 table.list grob 并以直方图与适当的汇总统计数据相匹配的方式对它们进行排序吗?table?我尝试使用 gList
进行组合,但它返回了 gList 中允许的错误 'only grobs',而且我还用 gTree 查找无济于事。
好吧,我终于想通了,而且看起来简单得令人尴尬。为了 combine/interleave 两组 grobs(虹膜直方图 iris.list
和汇总统计 tables table.list
)成一个 glist
可供 [=15 使用=] 你可以使用
marrangeGrob(grobs=(c(rbind(iris.list,table.list)))
最终结果是每种鸢尾花的单独直方图和摘要 table: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)))
我有代码可以在一组工作数据中生成一组直方图,我有代码可以为每个工作直方图生成一组摘要 tables,但是我无法将直方图和 table 组合起来。
在示例中使用虹膜数据:
#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"
更新:更改时错误已解决 marrangeGrob(grobs = list() to grobs = c()
感谢@Heroka
任何人都知道如何组合 iris.list 和 table.list grob 并以直方图与适当的汇总统计数据相匹配的方式对它们进行排序吗?table?我尝试使用 gList
进行组合,但它返回了 gList 中允许的错误 'only grobs',而且我还用 gTree 查找无济于事。
好吧,我终于想通了,而且看起来简单得令人尴尬。为了 combine/interleave 两组 grobs(虹膜直方图 iris.list
和汇总统计 tables table.list
)成一个 glist
可供 [=15 使用=] 你可以使用
marrangeGrob(grobs=(c(rbind(iris.list,table.list)))
最终结果是每种鸢尾花的单独直方图和摘要 table: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)))