R: gridExtra - How to plot a Summary as table?

不羁的心 提交于 2019-12-11 04:09:43

问题


I'm having trouble plotting a simple summary.

library(gridExtra)
SummaryTable <- summary(s.tvs$precio.nuevo)
grid.table(SummaryTable)

Gives me this:

I want to achieve something like this:


回答1:


Upgrade comment:

grid.table calls tableGrob.

grid.table
#function (...) 
#grid.draw(tableGrob(...))
#<environment: namespace:gridExtra>

From ?tableGrob its first argument is a matrix or data.frame. t coerces the named vector returned by summary to a matrix with dimension one row. Alternatively, you could of used as.matrix to produce a matrix with one column.

grid.newpage()
grid.table(t(summary(mtcars$mpg)))

grid.newpage()
grid.table(as.matrix(summary(mtcars$mpg)))

From comment:

Question: I'm trying to plot a barplot and the table generated in this answer. I get: Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList" when using this code: grid.arrange(a, tbl, ncol = 1)

To combine different tables / plots using grid.arrange they need to be grobs (grid GRaphcal OBjects). So you cannot pass the results from grid.table to grid.arrange as it is not a grob (it actually plots the tableGrob directly). For this you need to pass the tableGrob.

So for example:

mybar <- qplot(mtcars$mpg, geom="bar")
tbl <- tableGrob(t(summary(mtcars$mpg)))
grid.newpage()
grid.arrange(mybar, tbl) 


来源:https://stackoverflow.com/questions/32926718/r-gridextra-how-to-plot-a-summary-as-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!