How to print/plot a table in #R?

Asked

Viewed 1,272 times

2

Consider:

 dat <-  matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)

In which dat refers to data from the result of a loop, generating a new dat every cycle. I need to present these results, for this, I thought of exporting dat for a tabular format or an image.

inserir a descrição da imagem aqui

How can I generate this information/result in R as an already formatted table (margin lines and header) or image, this is possible?

  • 3

    It is not clear what you want. You want to plot on a graph, like figure, or print on the screen with a friendlier formatting?

  • 1

    As soon as I can I will improve the question. Thank you!

1 answer

1


Using as reference that reply from Soen, I came to the next:

First of all, make sure you really want to do this. Text and formatting are much more flexible and easy to modify than an image.

A simple way to do what you want is to use the package gridExtra:

library("gridExtra")

dat <- matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)
grid.table(dat)

Upshot:

inserir a descrição da imagem aqui

The table has run out of titles because it is a matrix without colnames. You can customize the image, but the syntax is not so simple. To see what can be done, you need to investigate the default theme values:

> ttheme_default()
$core
$core$fg_fun    
...    
$core$fg_params
$core$fg_params$parse
[1] FALSE
$core$fg_params$col
[1] "black"

$core$fg_params$fontsize
[1] 12
...

You can customize the theme as follows:

mytheme <- ttheme_default()
mytheme$core$bg_params$fill <- c("yellow", "pink")
grid.table(dat, theme = mytheme)

That would lead to this result:

inserir a descrição da imagem aqui

If you really want to invest in this, take a look at the help (?grid.table) and in the default theme to see what can be modified.

  • Immensely grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.