[R] Q: obtaining non-transparent background in png
D. R. Evans
doc.evans at gmail.com
Tue Jul 31 19:33:02 CEST 2007
On 31/07/07, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> On Tue, 2007-07-31 at 10:22 -0600, D. R. Evans wrote:
> > I am not understanding something about generating PNG plots.
> >
> > I have tried several ways to obtain something other than a transparent
> > background, but nothing I've done seems to change the background.
> >
> > For example:
> >
> > dev.print(png, width=800, height=600, bg='red', filename='example.png')
> >
> > which I thought would give a red background, simply gives the same
> > transparent background I always get.
>
> ?dev.print says:
>
> 'dev.print' copies the graphics contents of the current device to
> a new device which has been created by the function specified by
> 'device' and then shuts the new device.
>
> Note "copies" - given that you've already drawn a figure with a white
> background, should this then produce one that is red?
Well, I wondered about that, so the first thing I did was to test it
by changing a different parameter. I created an X11 plot with the
default size (480, I think?) and then printed it to a png with a width
of 800. That indeed created a PNG file of width 800, so I deduced from
that that it was OK to change the parameters of the plot in the
destination device.
> Not sure whether this is as intentional or not, but it does not appear
> to be passing the bg argument on to the 'device', or if it does, it is
> not being used/respected - perhaps all that is need is clarification as
> to what can be specified in '...' in ?dev.print
I think so. Either that or it seems to be a bug (I obviously don't
know enough about how things are supposed to work to make that
determination; but it does seem rather bug-like behaviour, especially
since one can certainly change some of the parameters associated with
the plot).
Anyway, it seems like I need an explicit "par(bg='red')" before
performing any graphical operations. That seems to do the trick.
Although it's still not clear how one would solve the general problem
in which one has an X11 plot with background colour A, but wants to
copy it to a PNG with background colour B...
Weirdly (at least it seems weird to me) I just tried the following
with an unexpected result:
I created the following function:
"to.png" <-
function(FILENAME = 'Rplot%03d.png')
{ par(bg='blue')
dev.print(png, width=800, height=600, filename=FILENAME)
}
par(bg='red')
plot(1:10) # ok, I get a red plot
to.png('should-be-blue.png') # png is red, not blue
plot(1:10) # now it's blue (as I sort-of expected)
So it isn't obvious that there's anything one can put in the to.png()
function that will control the colour of the background for the PNG
output.
More information about the R-help
mailing list