I’ve been looking for an hour, but everything I can find about how to make a nice looking table out of a data frame mentions that it’s for rmarkdown, html, or latex.
Is it not possible to make a nice looking table in base r?
plot(x, y) makes a graph.
Is there no function like: printTable(df)?
Broadly speaking over what you can get from a normal print in base::print there is not much else you can do. You could try to twist plot function to plot values from selected cells in a data frame but that would be very onerous to develop and impractical in the light of currently available and maintained solutions. There is a number of packages that let you achieve what you need. For instance you can try formattable by renkun-ken.
Example
For a simple example you can try formattable::formattable(mtcars[1:10,])
Creating Images
For a solution creating images from tables, have a look at this discussion. As discussed, in the linked answer if you insist on generating a static image you can use grid.table function offered via gridExtra: tbl <- grid.table(mtcars[1:5,]).
You may be interested in the flextable package that is very easy to use with multiple options to create nice tables.
You can also have multiple word, pdf, or html output types.
I invite you to check the manual : https://cran.r-project.org/web/packages/flextable/vignettes/overview.html
Related
In our production environment, we use gtsummary package to summarize large data, then convert to gt object to add title and subtitle. The visual quality of the resulting table is unparalleled.
However, in many instances we need this summary table accompanied by a companion data table that carries detail of a handful of outliers or similar specifics that clarify the overall. In a sense, this companion data is a table-based footnote, or clarifying support information.
Because we emit large numbers of these summaries, I am looking for a way to keep them together in the same emitted single-page pdf.
I've tried to solve this with the following:
tbl_merge, tbl_stack in gtsummary: gtsummary tables, not gt objects, requires shared rows or columns.
gridExtra: gt objects cannot be converted to grobs
pdf() or print to device: gtsave or print(gt) does not output to device.
Does anyone know if there is there a way to embed one table as a png object in the footnote of the other? Or is there another alternative?
Found a really clean solution to this problem using the gt package. We needed to avoid converting tables into images, which strips the content of much functionality including machine search.
The solution: Tranform the supplemental table into html using as_raw_html(), then insert into the primary table as source_note using tab_source_note(source_note = html(suppTable).
This retains all formatting, including titles and footnotes for each of the tables.
There are a range of tools available for creating publication quality tables using R, Sweave, and LaTeX.
In particular, there are helper functions like latex in the Hmisc package, and xtable in the xtable package. I've also often written my own code so that I could have complete control over table formatting (e.g., see this example).
However, when preparing publication quality tables a range of issues often arise:
how and when to apply numeric formatting
how to precisely control alignment of columns and cells
how to precisely control cell borders
how to convert variable labels to variable names
and so on
Beyond the high level issues of specifying the desired table format, there are issues of implementation.
When should a helper function such as xtable be used?
Which helper function should be used in a given situation?
How can the default output of helper functions be customised to particular requirements?
Question
It seems to me that the above issues are deserving of a detailed textbook-style introduction.
Are there any online or offline resources that provide a detailed overview of how to produce publication quality tables using R, Sweave, and LaTeX, and that address the issues discussed above?
Just to tie this up with a nice little bow at the time of current writing, the best existant tutorials on publication-quality tables and usage scenarios appear to be an amalgamation of these documents:
A Sweave example (source)
The Joy of Sweave: A Beginner's Guide to Reproducible Research with Sweave (source)
Latex and R via Sweave: An example document how to use Sweave (source)
Sweave = R · LaTeX2 (source)
The xtable gallery (source)
The Sweave Homepage
LaTeX documentation
Going beyond the scope of what currently exists, you may want to ask the author of The Joy of Sweave for a document on publication-quality tables specifically. It seems like he's gone above and beyond this problem in his research. In addition to the questions you've raised, this space specifically could use a style guide that, flatly, does not currently exist.
And, as mentioned in the question errata, this is a perfect example of a question for https://tex.stackexchange.com/. I encourage you to continue to ask specific questions there when you run into any difficulties in your current projects.
The package stargazer can create publication-quality - incl. using templates designed to resemble existing academic journals - from commonly used R statistical functions and packages (lm, glm, plm, svyglm, survival, pscl, AER, and others). Also good for creating summary statistics tables, and can directly output data frame content as well.
There is a tabular function in the tables package which addresses formatting, alignment and label operations. The package has a vignette which is a good starting point.
xtable has worked fine for me so far.
In combination with siunitx, and when necessary, longtable, it can produce pretty effective tables, in my opinion. With packages like booktabs and caption, the aesthetics can be pleasing too.
I am not sure this level of detail was asked for by the OP, but for what it's worth, the basic implementation could be something along these lines: https://tex.stackexchange.com/questions/41067/caption-for-longtable-in-sweave/41183#41183 (my own answer to another question).
I highly recommend ConTeXt which makes use of the TABLE package. There is a Table overview in contextgarden and an exhaustive manual.
Recently I was exposed to the grid.table function in the package gridExtra in R. I am simply looking to make my dataframes look "better" than simply printing them to the console.
The default settings work pretty well, but now I am getting greedy. I am asking for help because I am not sure if the following two formatting settings are possible. I read the documentation but am still not sure if this can happen...
Conditionally format/highlight rows and columns if they meet a certain criteria. My thinking was that I could highlight a certain row to make it stand out from the rest of the table.
Is it possible to left alight the first column in the table and center align everything else?
In short, I really like how easy it is to format a dataframe as a table, but now I am hoping I can do a few extra formatting techniques to get the tables to where I need them to be.
Having just emailed Baptiste the author of gridExtra, that functionality is not currently available.
However, he says that once the gtable package is officially released he is planning to make an update to grid.table which should make it easier to customise the output. But also says not to "hold your breath"...He also however suggested to feel free to use the source code and adapt it.
But I too have been looking for ways to highlight rows in tables and have used the package dprint to do so. Hope that helps.
I saw an interesting chart on engadget today made by Nielsen:
http://www.engadget.com/2011/07/28/nielsen-android-leads-us-smartphone-market-with-39-percent-shar/
original source: http://blog.nielsen.com/nielsenwire/online_mobile/in-u-s-smartphone-market-android-is-top-operating-system-apple-is-top-manufacturer/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+NielsenWire+%28Nielsen+Wire%29
I'd love for someone to replicate it if possible and show the R code. Basic packages or ggplot2 would be great.
I like that the boxes are proportional, that's a key feature :)
Thanks!
You can find several implementations in R under the name of 'mosaic chart'. E.g.:
require("vcd")
data(HairEyeColor)
mosaic(HairEyeColor, shade = TRUE)
Se some examples on e.g. quickR, but searching the R graph gallery is also a good option.
In ggplot2, you can find a sample on learnr's blog.
I have also done some tweaks in ggplot2, please find the attached plot below. It is in Hungarian, but if you are interested, I could clean up the code and post is somewhere.
UPDATE: I have searched for my old script based on comment and uploaded it to pastebin. Sorry, no code clean up and it is quite messy, as I had to make it up for mass reporting from SPSS data files, but I hope you could use it. The usage is simple: load all functions (e.g.: run all lines in R with the source(...) function), and you could generate a mosaic chart of any data frame by specifying two variable names in the parameters of ggMosaicChart(). The plot will be saved to a png file in the working directory (no easy resize in R of the plot as lots of manual tweaks are done to arrange text nicely).
I have translated the strings to English, a basic example (included in the above code) of the mtcars data set:
Count, row- and column percent and also Pearson residuals are shown for each cell.
It's called treemap. R project has packages named "treemap" or "portfolio" for it. Here is how to do: http://flowingdata.com/2010/02/11/an-easy-way-to-make-a-treemap/
So a while back (6 months+) I saw a blog post where the author took a line graph someone had posted on the internet, fed the image into R, and used a function to convert the image into a data frame.
I've looked everywhere, and I can't seem to find this blog post (even though I'm sure I bookmarked it). So I was wondering if any of you had also read said blog post, or if someone knew of a quick and easy way to convert a line graph to a data frame in R?
Was this it? I searched for "R digitize plot". The package used is "ReadImages". For completeness, the steps listed were (see link):
library(ReadImages) #Load package
mygraph <- read.jpeg('plot.jpg') #Import image
plot(mygraph) # Plot the image
calpoints <- locator(n=4,type='p',pch=4,col='blue',lwd=2) # Calibrate the plot by selecting known coordinates
data <- locator(type='p',pch=1,col='red',lwd=1.2,cex=1.2) # Collect the data points in a dataframe
When you say 'the image as a data frame', do you mean you want to get back to the original data that made the line?
It's not R, but I've used Engauge Digitizer for this sort of thing:
http://digitizer.sourceforge.net/
Also look at the updateusr function in the TeachingDemos package. Once you have the image displayed as in Benjamin's post, you can use the updateusr function with the known points to change the user coordinates so that then the results from the locator function do not need any additional transformation.
As i write this, the digitize package and the ReadImages package are no longer available for R 3.0.2. Engauge Digitizer is a good option but if you still want to do this sort of thing in R, take a loook at http://rscriptsandtips.blogspot.no/
You can also use im2graph to convert graphs to data. It's free and available of Windows and Linux (http://www.im2graph.co.il).