问题
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