Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R save table as image

Tags:

plot

r

I would like to export a data frame as a (png) image. I've tried with this code, but the table get clipped vertically.

library(ggplot2) library(gridExtra)  df <- data.frame(a=1:30, b=1:30)  png("test.png") p<-tableGrob(df) grid.arrange(p) dev.off() 

Is there a way to avoid this behaviour without having to set manually the size of the image?

like image 229
Andrea Valsecchi Avatar asked Apr 29 '14 12:04

Andrea Valsecchi


People also ask

How do you save an image in R?

If you're running R through Rstudio, then the easiest way to save your image is to click on the “Export” button in the Plot panel (i.e., the area in Rstudio where all the plots have been appearing). When you do that you'll see a menu that contains the options “Save Plot as PDF” and “Save Plot as Image”.

How do I make a table in R?

We can create a table by using as. table() function, first we create a table using matrix and then assign it to this method to get the table format. Example: In this example, we will create a matrix and assign it to a table in the R language.


2 Answers

You can change this behavior by specifying a height and width.

png("test.png", height=1000, width=200) p<-tableGrob(df) grid.arrange(p) dev.off() 

Anyway, it is usually not very helpful to save tables as pictures.

like image 154
shadow Avatar answered Sep 18 '22 23:09

shadow


You can do like this:

library(gridExtra) png("test.png", height = 50*nrow(df), width = 200*ncol(df)) grid.table(df) dev.off() 
like image 37
Ning Avatar answered Sep 19 '22 23:09

Ning