简体   繁体   中英

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.

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:

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:

在此处输入图像描述

To prove some attempt at solving this issue, I resorted unsuccessfully to plotting the axes after the 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.

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.

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. (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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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