简体   繁体   English

如何在基本 R plot() 中 plot 正交向量?

[英]How to plot orthogonal vectors in basic R plot()?

I am trying to plot the vectors [0.7, 0.7] and [0,7, -0.7] in a way that it is visually obvious that they are orthogonal.我试图 plot 向量 [0.7, 0.7] 和 [0,7, -0.7] 在视觉上很明显它们是正交的。

Since R plots points (not vectors) the origin of the vectors will be cut-off unless I adjust the x-axis to include the origin:由于 R 绘制点(不是向量),除非我调整 x 轴以包含原点,否则向量的原点将被截断:

dat <- cbind(c(.7,.7),c(.7,-.7))
plot(dat, main = "data", xlim=c(0,.8), xlab=NA, ylab=NA, type ="n")
arrows(x0 = 0, y0 = 0, x1 = dat[1,1], y1 = dat[2,1], lwd = 5, col="purple")
arrows(x0 = 0, y0 = 0, x1 = dat[1,2], y1 = dat[2,2], lwd = 5, col="orange")

But on top of it I have different spacings between ticks in the x and y axis distorting the geometry of the vectors:但最重要的是,我在 x 和 y 轴上的刻度之间有不同的间距,从而扭曲了向量的几何形状:

在此处输入图像描述

To prove some attempt at solving this issue, I resorted unsuccessfully to plotting the axes after the plot:为了证明解决此问题的一些尝试,我尝试在 plot 之后绘制轴,但未成功:

plot(dat, axes = FALSE)
axis(side = 1, at = seq(0,0.8, 0.01))
axis(side = 2, at = seq(-.8,.8,0.05))
arrows(x0 = 0, y0 = 0, x1 = dat[1,1], y1 = dat[2,1], lwd = 5, col="purple")
arrows(x0 = 0, y0 = 0, x1 = dat[1,2], y1 = dat[2,2], lwd = 5, col="orange")

... not a pretty picture. ......不是一张漂亮的照片。

Specify the asp argument, which determines y/x asp ect ratio.指定asp参数,它确定y/x纵横比。

dat <- cbind(c(.7,.7),c(.7,-.7))
plot(dat, main = "data", xlim=c(0,.8), xlab=NA, ylab=NA, type ="n", asp=1)
arrows(x0 = 0, y0 = 0, x1 = dat[1,1], y1 = dat[2,1], lwd = 5, col="purple")
arrows(x0 = 0, y0 = 0, x1 = dat[1,2], y1 = dat[2,2], lwd = 5, col="orange")

在此处输入图像描述

You can find details on this argument from:您可以从以下位置找到有关此论点的详细信息:

?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. asp:如果 asp 是一个有限的正值,那么 window 的设置使得 x 方向上的一个数据单元的长度等于 asp * y 方向上的一个数据单元。

Note that in this case, par("usr") is no longer determined by, eg, par("xaxs"), but rather by asp and the device's aspect ratio.请注意,在这种情况下,par("usr") 不再由例如 par("xaxs") 确定,而是由 asp 和设备的纵横比确定。 (See what happens if you interactively resize the plot device after running the example below!) (看看如果您在运行下面的示例后以交互方式调整 plot 设备的大小会发生什么!)

The special case asp == 1 produces plots where distances between points are represented accurately on screen.特殊情况 asp == 1 会生成图,其中点之间的距离在屏幕上准确表示。 Values with asp > 1 can be used to produce more accurate maps when using latitude and longitude.当使用纬度和经度时,asp > 1 的值可用于生成更准确的地图。

try setting limits:尝试设置限制:

xlim = c(-.1, 1)
ylim = c(-.8, .8)

This will draw the full extent of the space your vectors are described by.这将绘制您的向量所描述的空间的全部范围。 If your goal is to constrain the proportions as well you can change the scope of the limits and not fill the whole space, but preserve the proportions for both axes如果您的目标也是限制比例,您可以更改限制的 scope 而不是填充整个空间,但保留两个轴的比例

xlim = c(-1, 1)
ylim = c(-1, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM