[R] Creating png of layered legend

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Jul 26 11:22:29 CEST 2011

On Tue, Jul 26, 2011 at 12:23 AM, Amelia McNamara
<amelia.mcnamara at stat.ucla.edu> wrote:
> I am trying to create a plot that has multiple plot characters for
> each point (e.g. a point within a triangle, a triangle within a
> square, etc). The workaround I have found to do this is by plotting
> twice, as in this example:
> x <- c(1.1, 2.3, 4.6)
> y <- c(2.0, 1.6, 3.2)
> plot(x, y)
> points(x,y, pch=20, col="red", cex=0.5)
> This works, but perhaps there is a better way to do it in one step? My
> main problem comes when I want to make a legend. The workaround I have
> here is to again plot twice, as in
> legend(x="bottomright", legend=c("Category 1"), text.col="white",
> title="Fairly long title", title.col="white", col=c("red"),pch=20,
> pt.cex=0.5, bty="o", bg="white")
> legend(x="bottomright", legend=c("Category 1"), title="Fairly long
> title", pch=1, bty="o")
> which produces the correct image in my Quartz window. I have to
> include the title and legend text in both versions, plotting the first
> one in white to avoid the text getting overplotted while still
> creating a legend border of the appropriate size.
> However, when I try to save this as a png, only the most recently
> plotted legend is captured. Saving as a pdf works, but the deliverable
> for this project is a png file and I would like to be able to produce
> them directly from R. Is there a way for me to capture both legends in
> a png? Or is there a better way for me to plot multiple symbols for a
> single point?

 Have you tried specifying the background colour as a transparent colour?

 try bg="#FF000000" - the first hex doublet is full transparency, the
next three doublets can be anything - in both.

 Note that your example looks fine for me as you gave it - I suspect
the MacOS quartz PNG driver is doing something different to the Xlib
driver. Can you use type="Xlib" on your png() function call? What's
your R version? And sessionInfo() output?


More information about the R-help mailing list