PNG table |
Creating the Table
After loading the data, let's first use this trick to put line breaks between the levels of the effect variable. Depending on your data, you may or may not need or want to do this.
library(OIdata) data(birds) library(gridExtra) # line breaks between words for levels of birds$effect: levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect))
Next let's make our table:
xyTable <- table(birds$sky, birds$effect)
plot.new() grid.table(xyTable, # change font sizes: gpar.coltext = gpar(cex = 1.2), gpar.rowtext = gpar(cex = 1.2))
Now you can view and save the image just like any other plot.
The code is available in a gist.
Citations and Further Reading
- http://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r
- http://stackoverflow.com/questions/12518387/can-i-create-an-empty-ggplot2-plot-in-r
Hi,
ReplyDeleteI'm very thankfull for your code, but could you suggest a way to speedup the plotting?
Currently I'm working on an algorithm which merges certain rows of a dataframe, according to the values inside the data.frame.
I would like to plot the intermediate dataframes to visualise the process of merging.
But currently the plotting is taking longer than the merging...
Do you need them as images? If not, it would be easier to export them as text or LaTeX
ReplyDeletehttp://stackoverflow.com/questions/5465314/tools-for-making-latex-tables-in-r
why do you use ggplot2 at all here? grid.table on its own can be viewed and saved to a file just as well.
ReplyDeleteThanks! I didn't know about grid.table. I changed the blog post to use this simpler way. The ggplot version is still available in the gist.
ReplyDeleteplot.new() should rather be grid.newpage()
ReplyDelete