简体   繁体   中英

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)

For example suppose I've got a data.frame like this one:

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?

The tableGrob() function in GridExtra may help.

> 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM