[R] Figures within tables [slightly off-topic]

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Apr 13 13:48:44 CEST 2010


On 13-Apr-10 09:54:20, Jim Lemon wrote:
> On 04/13/2010 06:07 AM, Peter Jepsen wrote:
>> Dear R-listers
>>
>> I am writing a manuscript for a scientific journal in clinical
>> medicine. I have three groups of patients, and I present a 10*3
>> table of their characteristics in Table 1. Some of their
>> characteristics, e.g. their age, are on a continuous scale,
>> others are dichotomous. I am thinking of presenting the age
>> distribution in each group as miniature graphs, each of which
>> must fit in one table cell. I am hoping that someone can
>> answer these questions:
>>
>> 1. Has anybody ever seen something like this published anywhere?
>>
>> 2. Should I draw the entire table as a figure, or should I make
>> a table in Word (or similar) and manually insert the graphs in
>> their cells?
>>
>> 3. Are there R packages that can "draw tables"?
>>
>> 4. And one for you editors out there: Would such a table count
>> as one figure, several figures, or a table?!
>>
>> Forgive me for being somewhat off-topic. I hope for your help anyway.
>>
> Hi Peter,
> This sounds like Edward Tufte's "sparklines" and I think there
> was a discussion of this sort of graphic a couple of years ago.
> Yep, see:
> 
> http://finzi.psych.upenn.edu/R/Rhelp02/archive/76508.html
> 
> Jim

It does indeed sound like "sparklines"! In the posting by Robin
Hankin that Jim refers to, the URL for Tufte's illustration of
sparklines is:

http://www.edwardtufte.com/bboard/
q-and-a-fetch-msg?msg_id=0001OR&topic_id=1

[equivalently: http://tinyurl.com/6bbkw ]

Robin posted that he would send privately to the original poster
examples of how to do it in Sweave. It could be useful if Robin
could provide a pointer to on-line examples of such usage.

In general, the key to getting that sort of thing right is to
prepare the graphic beforehand as an Encapsulated PostScript (EPS)
file, which R can do very nicely. This can then be imported into
document-preparation software which can handle EPS files properly.
LaTeX can do that, also the classic groff (always present on Linux
systems). Maybe Word can, but don't ask me about that; however,
if you live in that world then R should be able to produce Windows
Metafiles (WMF) of graphics which could then be imported.

The point here is that an EPS file contains a %%BoundingBox line
which states what the dimensions of the graphic are in terms of
the within-file coordinate system (specifically, location of
lower left and upper right corners). The important thing about
that is that the software which produces the EPS file should
compute a good BoundingBox -- if that is wrong, then the result
can be awful. R is good at that, though you may need to tweak
things using relevant options in the postscript() command. You
may even need to explicitly edit the %%BoundingBox line in the
EPS file to get it exactly as you want it!

Once that is available, the document software will alow you to
specify somehow the dimensions of the graphic as it will appear
in the text. I'm no expert on LaTeX, so can't discuss details
of this. Maybe other will; maybe Robin's examples will explain.

In groff, however, you can specify these directly, or indirectly
via a macro, and then import the graphic where you want; and you
can even define a special "character" which will display as the
graphic and will also have the same status as any ther character
(e.g. the letter "A") and be automatically placed, and scaled
according to the current text point size, just like an ordinary
text character.

Either way, there is no problem putting a graphic into a cell
in a table.

As it happens, I received via the groff mailing-list a few months
ago a query from someone who precisely wanted to implement Tufte
sparklines in running text. He provided and example of text and
of a sparkline graphic. I was then able to create an example of
how this works in groff. He gave permission for his example and
the graphic to be used in communicating the method communicated
to others, so I have placed on my website

A: An explanation of how it is done, with illustrations using
   his EPS graphic:

   http://www.zen89632.zen.co.uk/Misc/do_spark_howto.pdf

B: The example EPS file he provided:

   http://www.zen89632.zen.co.uk/Misc/spark.average.fund.size.eps

Note that, owing to peculiarites of the graphic (in particular
rthe X-axis) it does not render well in the PDF file -- you may
need to zoom to 200% to see it properly.

This is an example of importing an EPS file into running text,
with adjustable vertical and horizontal dimensions, in other
words a true Tufte "sparkline".

Hoping this helps, at least for explanation!

As to what editors may think of it -- Is it text? Is it graphics?
Is it a table? I just don't know. They tick their own boxes.
It is certainly playing the *rôle* of text, but the fact that
there needs to be a graphics (e.g. EPS) file present in order
for it to work means that their "graphics" light may flash red.
And in your application it is certainly occurring in the context
of a table. I'd go for "table" myself (with arbitrary content
in the cells), but that's no guide!

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Apr-10                                       Time: 12:48:42
------------------------------ XFMail ------------------------------



More information about the R-help mailing list