Opaque legend on R chart with transparent background?

I am trying to put a legend in an R plot, on top of some grid lines. I want the legend to be opaque so that the gridlines are not drawn over the legend. However, I also want the entire image to have a transparent (not white) background for display on the web.

I know what I can do bg="white"

to get an opaque legend, but it doesn't match with png(..., bg=transparent)

. Example:

    png("test.png", bg="transparent")
    plot(rnorm(10))
    grid()
    legend("topright", legend="foo", bg="white")
    dev.off()

      

When this png is displayed on a website with a colored background, the color will appear on most of the graph, but the legend will still be white. Is there a way to make the legend "opaque" so that the gridlines don't show up but are still transparent in the final png output?

+3


source to share





All Articles