Gimelist
Gimelist

Reputation: 823

Extract plotting region coordinates for grid.picture() in R

Background

I am plotting the following image:

enter image description here

Using this code:

par(mfrow = c(2, 2),
    mar = c(2, 2, 2, 2))

plot(1, col = "red", pch = 15)
plot(1, col = "blue", pch = 15)
plot(1, col = "orange", pch = 15)
plot(1, col = "purple", pch = 15)

I'd like to add an external postscript file using grImport (sample data included):

library(grImport)
#PostScriptTrace("drawing.ps")
#pic <- readPicture("drawing.ps.xml")

# generated with dput()
pic <- new("Picture",
           paths = list(path = new("PictureFill", rule = "nonzero",
                                   x = c(move = 0, line = 1315.47, line = 1315.47, line = 0, 
                                         line = 0),
                                   y = c(move = 1080, line = 1080, line = 7.37891, 
                                         line = 7.37891, line = 1080),
                                   rgb = "#D0D1E6", lty = numeric(0), 
                                   lwd = 10, lineend = 1, linejoin = 1, linemitre = 10)),
           summary = new("PictureSummary", 
                         numPaths = 1, xscale = c(xmin = 0, xmax = 1315.47),
                         yscale = c(ymin = 7.37891, ymax = 1080)))

grid.picture(pic)

This just puts the ps image filling the entire plotting device:

enter image description here

Question

I would like the violet square to plot exactly on top of one of the plotting regions (let's say top left).

How do I extract the coordinates of the first plot() in the reference frame of the entire device?

Once I have those, how to correctly use the coordinates in grid.picture()?

Upvotes: 2

Views: 141

Answers (1)

Paul Murrell
Paul Murrell

Reputation: 361

Probably too late to help, but, as a starting point, you could use picture() rather than grid.picture(), for example ...

    mar = c(2, 2, 2, 2))

plot(1, col = "red", pch = 15)
usr <- par("usr")
picture(pic, usr[1], usr[3], usr[2], usr[4])
plot(1, col = "blue", pch = 15)
plot(1, col = "orange", pch = 15)
plot(1, col = "purple", pch = 15)

That will distort the imported image to fill the plot region, but if that is a problem more work can be done to improve that solution.

Upvotes: 1

Related Questions