【发布时间】:2020-01-28 22:55:18
【问题描述】:
我想生成一个图表,该图表将使用基础 R 在绘图中显示“矢量场”。
脚本的这一部分将生成一个带有不同圆的图形(仅改变它们的半径来构成矢量场的底)。
r = 100 # Set the maximum radius to make an empty plot
x = function(ang) r* cos(ang) # To draw circles
y = function(ang) r* sin(ang) # To draw circles
nb = seq(from = 0,to = (2*pi),length.out = 100) # To have a sequence to draw the circles
plot(x(nb),y(nb), # Empty plot
asp = 1,
type = "n",
bg = "black",
col = "black",
pch =21, main = "Circle",
ylab = "Y values",
xlab ="X values")
abline(h=0,v=0) # Draw axes
for (i in seq(0,100,by = 5)) { # Draw a series of circles
r = i
points(x(nb),y(nb),
type = "l",
lwd = 1.0,
lty = 3)
}
# DRAWING TE VECTORS ----------------------
by = 10 # Define a "resolution" to see better the circles (This value will be smaller to be more precise)
changex = seq(0,100, by =by) # For each circle draw a radius with this sequence
current = -1 # This is to "flip" the orientation of the vectors
mag = current* seq(100,0, by = current*by)
arrows(x0 = changex, y0 = 0, # Draw the vectors
x1 = changex, y1 = mag,
code = 2,
length=0.1,
angle=40)
其余代码在改变图形角度时尝试打印向量:
xycircle <- function(ang,r) { # function to draw position on the circle
x = r*cos(ang)
y = r*sin(ang)
return(list(x,y))
}
pilist = c(#0,1/4*pi,#1/2*pi, # List of PI values to go around the circle
#pi, #3/4*pi,
#3/2*pi,
2*pi)
for (pip in 1:length(pilist)) { # Going around the circle
ang = pilist[pip] # extract 1 angle value to draw
abline(a=0,b=tan(ang), lty = 3, lwd = 3) # Get a line that will show the angle selected
r = seq(0,100, by = by) # List of radius
mag = current* seq(-100,-0, by = by) # Magnitude of the vectors
for (i in 1:length(r)) { # Draw vectors when the angle changes
arrows(x0 = xycircle(ang,r[i])[[1]], # Base position of the vector (tangent to the circle)
y0 = xycircle(ang,r[i])[[2]],
x1 = cos(atan2(r[i],mag[i])-ang)*sqrt(r[i]^2+mag[i]^2), # Position of the tip of the vector (x)
y1 = sin(atan2(r[i],mag[i])-ang)*sqrt(r[i]^2+mag[i]^2), # Position of the tip of the vector (y)
code = 2, # Change the arrow head
length = 0.1,
angle = 40)
}
}
如您所见,当我完成一整圈时,向量未与原始向量对齐(它们应该...)。
如何使向量围绕圆“旋转”(基于角度)以使它们围绕圆旋转,以便向量始终垂直于圆(如上一张图,但所有角度) .
【问题讨论】: