[英]How can I visualize a data.frame with a values column and a label column in R?
I'm using the pdf
command and ggplot2
to create couple different types of graphs and while I'm at it I'd like to throw in some simple tables (with, for example, column labels being coefficient names and rows having values) but I'm not sure to make a "plot" out of that without going separately into excel to make a table (but then I don't know how to insert it into the pdf I generate with R) 我正在使用pdf
命令和ggplot2
创建几个不同类型的图形,而在此过程中,我想抛出一些简单的表(例如,列标签为系数名称,行具有值),但是我不确定在不单独进入excel来制作表格的情况下做出“绘图”(但是后来我不知道如何将其插入用R生成的pdf中)
For example suppose I've got a data.frame
like this one: 例如,假设我有一个像这样的data.frame
:
set.seed(1)
foo = data.frame(val1=rnorm(5), val2=rnorm(5), columnLabels=c('A','B','C','D','F'))
Is there a simple way to "plot" a simple table with those column labels, with row labels like c('Val 1', 'Val2') and with the corresponding values? 是否有一种简单的方法来“绘制”具有这些列标签,具有c('Val 1','Val2')等行标签以及相应值的简单表?
The tableGrob() function in GridExtra may help. GridExtra中的tableGrob()函数可能会有所帮助。
> library(grid)
> library(gridExtra)
> set.seed(1)
> d = data.frame(val1=rnorm(5), val2=rnorm(5), columnLabels=c('A','B','C','D','F'))
> # transpose dataframe to get requested columns/rows
> rownames(d) <- d$columnLabels
> d$columnLabels <- NULL
> dt <- t(d)
> g <- tableGrob(dt)
> grid.newpage()
> grid.draw(g)
Which is basically 基本上是
> example(tableGrob)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.