[R] pie EPS BB

Marc Schwartz marc_schwartz at me.com
Wed Mar 10 23:19:13 CET 2010


On Mar 10, 2010, at 4:03 PM, Ted Harding wrote:

> Greetings all!
> I'm facing a puzzle I have not been able to solve.
> I need to make an EPS of a pie-chart (Yes, I know;
> please don't bother to tell me! I just need to ...).
> 
> I'm trying to do it with pie(), and I want to have
> just the plain pie-chart with no annotations. So far
> so good: "labels=rep(NA,...)" will do it.
> 
> But I want to have it output to an EPS file with the
> BoundingBox *exactly* containing the pie-chart, i.e.
> the pie-chart boundary touches the four sides of the
> BoundingBox, and its centre is at the centre of the
> BoundingBox. This is the puzzle I have not been able
> to solve.
> 
> Here is an example of the sort of thing I have been trying.
> 
> pcts <- c(3.0,2.0,0.4,10.0,12.0,3.0,39.0,14.0,7.0,9.6)
> postscript("piecharttest.eps",horizontal=FALSE,
>           paper="special",width=3.0,height=3.0)
> pie(pcts, clockwise=TRUE, labels=rep(NA,10), radius=1.0)
> dev.off()
> 
> With
>  pie(pcts, clockwise=TRUE, labels=rep(NA,10), radius=1.0)
>  lines(c(-1,1,1,-1,-1),c(-1,-1,1,1,-1))
> I can see that the chart exactly fits inside a box with
> side 2 and centre (0,0). So far so good. But this box is
> not centred in the X-window device (somewhat up, and
> somewhat to the right, from centre).
> 
> The result from the above postscript() command is a file
> with
> 
> %%DocumentMedia: special 216 216 0 () ()
> %%BoundingBox: 0 0 216 216
> 
> (where of course "216" means 3*72 points = 3 inches), and
> when this is displayed (using 'gv') I see the pie-chart
> distinctly offset from the centre of the BoundingBox,
> with its leftmost point at approx X=84 points from the left
> of the box (at 0), its rightmost point at X=162 approx
> 54 points from the right of the box (at 216), so the diameter
> of the circle is approx 162-84 = 78 points (a bit over 1 inch),
> and the centre of the circle is approx at 123 points (X)
> which is 15 points to the right of the centre of the box.
> Similarly the top of the circle is at approx Y=156 (60 points
> from the top of the box) and the bottom is at approx Y=78
> from the bottom of the box.
> 
> So, although relative dimensions are about right as measured,
> the pie-chart is off-centre relative to the BoundingBox,
> just as when displayed in the X-window.
> 
> I have been able to re-write the %%BoundinBox line by editing
> the EPS file until it looks about right, and can then continue.
> However, I would like to be able to exactly enclose the pie
> chart in the BoundingBox automatically, but the documentation
> (in ?pie and in ?postscript) seems to contain nothing that
> would give me a clue for achieving this.
> 
> By way of background: I want to include the EPS file in a
> document where I would use graphics commands to embellish
> the pie-chart with my own annotations. For this I need to
> be able to calculate the cordinates of points on the chart
> relative to its centre; whereas the software will be using
> the BoundingBox to locate the imported graphic. So I have
> to be able to work out the coordinates of the pie-chart
> relative to the BoundingBox. The PostScript code in the EPS
> file is difficult to decipher from this point of view, so
> (as explained) I have been reduced to editing the Bounding
> Box until it looks about right.
> 
> Sorry for the long account, but I wanted to try to make the
> situation clear.
> 
> With thanks for any suggestions!
> Ted.


Ted, try this:

pcts <- c(3.0, 2.0, 0.4, 10.0, 12.0, 3.0, 39.0, 14.0,
          7.0, 9.6)

postscript("piecharttest.eps", horizontal = FALSE,
           onefile = FALSE, paper = "special", 
           width = 3.0, height = 3.0)

par(mar = c(0, 0, 0, 0), xaxs = "i", yaxs = "i")

pie(pcts, clockwise = TRUE, labels = rep(NA, 10), 
    radius = 1.0)

dev.off()


Two things:

1. Set the margins to 0 on all four sides and set the axes to 'i' so that they don't expand by the default 4%.

2. You forgot the 'onefile = FALSE' in the call to postscript().

HTH,

Marc Schwartz



More information about the R-help mailing list