gpt4 book ai didi

r - 对R中的xlim/ylim行为感到困惑

转载 作者:行者123 更新时间:2023-12-04 06:18:48 31 4
gpt4 key购买 nike

R-3.1.1,Win7x64

嗨,我有测量两个变量的数据,使得X从0到70,Y从0到100。我想对观察值做一个简单的散点图。
散点图应进行尺寸设计,以使x轴(从0到70延伸)为y轴大小(从0到100延伸)的0.7。

我使用以下代码

plot.new()
plot(0, 0, asp = 1, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "", type = "n")

我很惊讶地看到它产生如下图:

有两件事与我的预期不符:1)x轴和y轴不限于其xlim和ylim值。 (为什么?)和2)数字几乎是正方形的。

我可以在使用代码之前通过手动调整R窗口或Rstudio窗口的大小来手动调整图形的大小,但这是不可行的,因为我要绘制很多图形,其中许多xlim和ylim大小都不同,并且这些图形需要稍后再插入预先格式化的报告中(这就是为什么它们需要满足这些确切的布局要求)的原因。
我也尝试使用
dev.new(width = 7, height = 10)

但这也无济于事。

我的问题是:
1)我怎样才能“强制”将图形限制为传递给函数的确切xlim和ylim范围?

2)如何生成具有精确相对尺寸的图形(x轴的宽度是y轴长度的0.7倍)

最佳答案

asp的帮助中对plot.window的讨论意味着asp将覆盖xlimylim设置(如果您查看plot的帮助,它将指导您进入plot.window以了解有关asp的更多信息):

If asp is a finite positive value then the window is set up so that one data unit in the x direction is equal in length to asp * one data unit in the y direction.

Note that in this case, par("usr") is no longer determined by, e.g., par("xaxs"), but rather by asp and the device's aspect ratio. (See what happens if you interactively resize the plot device after running the example below!)

The special case asp == 1 produces plots where distances between points are represented accurately on screen. Values with asp > 1 can be used to produce more accurate maps when using latitude and longitude.



就像@ mr.joshuagordon指出的那样,您可以使用 pdf(如果需要位图输出,则可以使用 pngjpeg)函数,并使用尺寸来获取所需的宽高比,同时从 asp中删除 plot参数,以便可以设置 xlimylim值。

另一个选项是切换到 ggplot2,这使得可以轻松地分别设置轴限制和纵横比:
library(ggplot2) 

# Some fake data
dat = data.frame(x=c(2,30,50), y=c(10, 60, 90))

# 1 y-unit = 1 x-unit, so the plot area is not square
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(limits=c(0,70)) +
scale_y_continuous(limits=c(0,100)) +
coord_fixed(ratio=1)

# 1 y-unit = 0.7 * 1 x-unit, so the plot is square, but the physical distance
# of each x-unit and y-unit are no longer the same
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(limits=c(0,70)) +
scale_y_continuous(limits=c(0,100)) +
coord_fixed(ratio=70/100)

更新:这是在基础图形中独立控制 xlimylim和长宽比的方法:使用ogt_code图形参数设置图形区域的物理尺寸,而不是 asp。此设置不会影响 pinxlim的标称值,但会更改1 x单位和1 y单位的物理距离度量。这里有些例子:

示例1:我们将在一个PDF页面上创建两个面板,每个面板具有不同的宽高比:
# Create a 12" x 6" pdf graphics device 
pdf("test.pdf", width=12, height=6)

# Divide graphics device into two regions, each of which will contain a plot
par(mfrow=c(1,2))

# Left Panel: 5" x 5" plot area (plot region is square, so 1 y-unit =
# 0.7 * 1 x-unit in terms of physical distance in the plot region)
par(pin=c(5,5))
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n",main='par(pin=c(5,5)')

# Right Panel: 0.7*5" x 5" plot area (so 1 x-unit = 1 y-unit
# in terms of physical distance in the plot region)
par(pin=c(0.7*5,5))
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n",main='par(pin=c(5,0.7*5)')

dev.off()

示例2:如果将 ylim设置为大于图形设备的大小,则会显示错误消息。我们将使用默认设备(本例中为 pin)。
par(pin=c(10, 10))
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n",main='par(pin=c(5,4)')

# Get dimensions of the default plot device
par("din")

# Create a plot that takes up an area just a bit smaller than the device size
par(pin=par("din")-0.2)
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n",main='par(pin=c(5,4)')

# Create a plot that takes up an area just a bit larger than the device size
# (we'll get an error this time)
par(pin=par("din") + 0.01)
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n")

> Error in plot.new() : plot region too large

示例3:如果超过了 RStudioGD(或 pdf等)设备的大小,则会发生相同的错误:
# Create a 5" x 5" pdf graphics device
pdf("test.pdf", 5,5)
# Create a plot that takes up a little bit less than the device size
par(pin=c(5,5)-0.2)
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n")
dev.off()

# Create a plot that takes up a little bit more than the device size
pdf("test.pdf", 5,5)
par(pin=c(5,5)+0.01)
plot(0, 0, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "",
type = "n")
# Gives the following error: Error in plot.new() : plot region too large
dev.off()

关于r - 对R中的xlim/ylim行为感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25583189/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com