[R] Graphical table in R

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Jan 11 17:39:47 CET 2005


On Tue, 2005-01-11 at 14:59 +0000, Dan Bolser wrote:
> On 10 Jan 2005, Peter Dalgaard wrote:
> 
> >Dan Bolser <dmb at mrc-dunn.cam.ac.uk> writes:
> >
> >> Cheers. This is really me just being lazy (as usual). The latex
> function
> >> in Hmisc allows me to make a .ps file then grab a screen shot of
> that ps
> >> and make a .png file.
> >> 
> >> I would just like to use plot so I can wrap it in a png command and
> not
> >> have to use the 'screen shot' in between.
> >
> >A screen shot of a ps file? That sounds ... weird. If you can view
> it,
> >presumably you have Ghostscript and that can do png files.
> 
> The thing is the ps file has teh wrong size, so I end up with a small
> table in the corner of a big white page (using imageMagick convert
> function).
> 
> I havent tried ghostscript (don't know the cmd).
> 
> I could set the paper size correctly if I knew the size of my table,
> but I
> don't know how to calculate that before hand and feed it into the
> latex
> commands (Hmisc).
> 
> Seems like I should roll my own table with the plot command and
> 'primatives' (like the demo(mathplot)) - I just hoped that someone had
> already done the hard work for me and I could type something like...
> 
> plot.xtable(x)
> 
> x = any R object that makes sense to have a tabular output.
> 
> Seems like such a function done correctly could be usefull for helping
> people write up (hem) analysis.
> 
> Thanks again for the help everyone.
> 
> Dan.

Dan,

I think that taking Peter's/Thomas' solution provides a substantial
level of flexibility in formatting. I wish that I had thought of that
approach... :-)

For example:

  plot(1:10, type="n")

  txt <- capture.output(ftable(UCBAdmissions))

  par(family = "mono")

  text(4, 8, paste(txt,collapse="\n"))

  text(4, 6, paste(txt,collapse="\n"), cex = 0.75)

  text(4, 4, paste(txt,collapse="\n"), cex = 0.5)


Using par(cex) in the call to text() and modifying the x,y coordinates
will enable you to place the table anywhere within the plot region and
also adjust the overall size of the table by modifying the font size.

You can also use the 'adj' and 'pos' arguments in the call to text() to
adjust the placement of the table, so rather than being centered on x,y
(the default) it could be moved accordingly. See ?text for more
information.

Finally, you can even put a frame around the table by crudely using
strwidth() and strheight(). Some additional hints on this would be
available by reviewing the code for legend()...

# Do this for the first table (assumes 'cex = 1'):

# Get table width and add 10%
table.w <- max(strwidth(txt)) * 1.1

# Get table height (not including space between rows)
table.h <- sum(strheight(txt))

rect(4 - (table.w / 2), 8 - (table.h), 
     4 + (table.w / 2), 8 + (table.h))


It would take some work to combine all of this into a single function,
providing for additional flexibility in positioning, frame line
types/color/width, adjusting for 'cex' and so on. It could be done
though...

This is, in effect, taking an entire R character object and plotting it.

Does that help?

Marc




More information about the R-help mailing list