保留R的网格图形中的纵横比

时间:2022-08-27 10:50:18

To draw a "crossed" rectangle of height 2 times larger than its width using the low-level graphics package facilities I call:

要使用我认为的低级图形包设施绘制一个高度比其宽度大2倍的“交叉”矩形:

xlim <- c(0, 500)
ylim <- c(0, 1000)
plot.new()
plot.window(xlim, ylim, asp=1)
rect(xlim[1], ylim[1], xlim[2], ylim[2])
lines(c(xlim[1], xlim[2]), c(ylim[1], ylim[2]))
lines(c(xlim[1], xlim[2]), c(ylim[2], ylim[1]))

The figure has a nice feature: the aspect ratio is preserved so that if I change the size of the plot window, I get the same height-to-width proportions.

该图有一个很好的功能:保留纵横比,这样如果我改变绘图窗口的大小,我得到相同的高宽比。

How can I obtain an equivalent result with grid graphics?

如何通过网格图形获得等效结果?

2 个解决方案

#1


3  

You should create a viewport that uses Square Normalised Parent Coordinates, see ?unit:

您应该创建一个使用Square Normalized Parent Coordinates的视口,请参阅?unit:

"snpc": (...) This is useful for making things which are a proportion of the viewport, but have to be square (or have a fixed aspect ratio).

“snpc”:( ...)这对于制作视口的一部分,但必须是正方形(或具有固定的宽高比)的东西很有用。

Here is the code:

这是代码:

library('grid')
xlim <- c(0, 500)
ylim <- c(0, 1000)
grid.newpage() # like plot.new()
pushViewport(viewport( # like plot.window()
        x=0.5, y=0.5, # a centered viewport
        width=unit(min(1,diff(xlim)/diff(ylim)), "snpc"), # aspect ratio preserved
        height=unit(min(1,diff(ylim)/diff(xlim)), "snpc"),
        xscale=xlim, # cf. xlim
        yscale=ylim  # cf. ylim
))
# some drawings:
grid.rect(xlim[1], ylim[1], xlim[2], ylim[2], just=c(0, 0), default.units="native")
grid.lines(xlim, ylim, default.units="native")
grid.lines(xlim, rev(ylim), default.units="native")

The default.units argument in e.g. grid.rect forces the plotting functions to use the native (xscale/yscale) viewport coordinates. just=c(0, 0) indicates that xlim[1], ylim[1] denote the bottom-left node of the rectangle.

例如,default.units参数grid.rect强制绘图函数使用本机(xscale / yscale)视口坐标。 just = c(0,0)表示xlim [1],ylim [1]表示矩形的左下角节点。

#2


1  

In ggplot2 (which is grid based) you can fix the aspect ratio using coord_fixed():

在ggplot2(基于网格)中,您可以使用coord_fixed()修复宽高比:

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + coord_fixed(ratio = 0.5)

This will fix the ratio, and the ratio will be constant even when changing the size of the graphics window.

这将固定比率,即使更改图形窗口的大小,比率也将保持不变。

I'm not sure if this is helpful, as you asked for a low-level grid based solution. But I thought it might be useful none the less.

我不确定这是否有用,因为您要求使用基于网格的低级解决方案。但我认为它可能会有用。

#1


3  

You should create a viewport that uses Square Normalised Parent Coordinates, see ?unit:

您应该创建一个使用Square Normalized Parent Coordinates的视口,请参阅?unit:

"snpc": (...) This is useful for making things which are a proportion of the viewport, but have to be square (or have a fixed aspect ratio).

“snpc”:( ...)这对于制作视口的一部分,但必须是正方形(或具有固定的宽高比)的东西很有用。

Here is the code:

这是代码:

library('grid')
xlim <- c(0, 500)
ylim <- c(0, 1000)
grid.newpage() # like plot.new()
pushViewport(viewport( # like plot.window()
        x=0.5, y=0.5, # a centered viewport
        width=unit(min(1,diff(xlim)/diff(ylim)), "snpc"), # aspect ratio preserved
        height=unit(min(1,diff(ylim)/diff(xlim)), "snpc"),
        xscale=xlim, # cf. xlim
        yscale=ylim  # cf. ylim
))
# some drawings:
grid.rect(xlim[1], ylim[1], xlim[2], ylim[2], just=c(0, 0), default.units="native")
grid.lines(xlim, ylim, default.units="native")
grid.lines(xlim, rev(ylim), default.units="native")

The default.units argument in e.g. grid.rect forces the plotting functions to use the native (xscale/yscale) viewport coordinates. just=c(0, 0) indicates that xlim[1], ylim[1] denote the bottom-left node of the rectangle.

例如,default.units参数grid.rect强制绘图函数使用本机(xscale / yscale)视口坐标。 just = c(0,0)表示xlim [1],ylim [1]表示矩形的左下角节点。

#2


1  

In ggplot2 (which is grid based) you can fix the aspect ratio using coord_fixed():

在ggplot2(基于网格)中,您可以使用coord_fixed()修复宽高比:

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + coord_fixed(ratio = 0.5)

This will fix the ratio, and the ratio will be constant even when changing the size of the graphics window.

这将固定比率,即使更改图形窗口的大小,比率也将保持不变。

I'm not sure if this is helpful, as you asked for a low-level grid based solution. But I thought it might be useful none the less.

我不确定这是否有用,因为您要求使用基于网格的低级解决方案。但我认为它可能会有用。