'par('usr') equivalent command for grid?

When grid package is used to plot a figure, I want to find out something similar to par('usr') as in base R graphics. What command can do it?

https://r-graph-gallery.com/100-high-density-scatterplot-with-binning.html

For example, how to get the value equivalent to par('usr') in the following figure?

library(hexbin)
library(RColorBrewer)
 
# Create data
x <- rnorm(mean=1.5, 5000)
y <- rnorm(mean=1.6, 5000)
 
# Make the plot
bin<-hexbin(x, y, xbins=40)
my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))
plot(bin, main="" , colramp=my_colors , legend=F ) # the plot is based on grid if I understand it correctly.


Solution 1:[1]

Viewports in grid have information similar to what par() shows in base graphics, though with different names.

hexbin sets up viewports for the plot and the legend that it displays. To set the plot viewport as current, save the hexbin plot to a variable p, then use

pushHexport(p$plot.vp)

Now current.viewport() will return the grid viewport. str(current.viewport()) will show you what's stored in it. In particular, xscale and yscale contain the limits of those two axes, similar to par("usr")[1:2] and par("usr")[3:4].

The hexbin package gives access to some of these things directly in the p$plot.vp object: see class?hexVP for details. In particular, p$plot.vp@xscale gets the xscale property of the viewport.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2554330