繁体   English   中英

如何在R中绘制向量?

[英]how to plot a vector in R?

我想在图表上绘制旧向量和新向量。 我需要使用 par 函数还是其他什么?

    programme<-function(a,old_vector){
#there would be two input: V(the vector to be rotated), a(the angle of rotation)

    old_vector=matrix(c(cos(a),sin(a),sin(a),cos(a)),nrow=2)
#the matrix is 2*2 matrix

    rotated_vector=old vector%*%old_vector
#To rotate a vector (𝑥,𝑦)𝑇 widdershins (anticlockwise) by 𝜃 radians,

    return(rotated_vector)}

我认为您的旋转功能需要进行一些编辑,因此我编辑了该代码。

programme<-function(a,old_vector){
 
  
  rotate_matrix <- matrix(c(cos(a),sin(a),-sin(a),cos(a)),nrow=2)
 
  
  rotated_vector <- rotate_matrix %*% (old_vector)
 
  
  rotated_vector
}
programme(pi/3, c(1,0))
          [,1]
[1,] 0.5000000
[2,] 0.8660254

要绘制这两个向量(原始,旋转),请尝试此功能

programme_plot<-function(a,old_vector){
 
  
  rotate_matrix <- matrix(c(cos(a),sin(a),-sin(a),cos(a)),nrow=2)
 
  
  rotated_vector <-  as.vector(t(rotate_matrix %*% (old_vector)))
 
  dummy1 <- rbind(c(0,0), old_vector)
  dummy2 <- rbind(c(0,0), rotated_vector)
  {plot(c(0, old_vector[1]), c(0, old_vector[2]), type="l", 
        xlim = c(min(0,old_vector[1], rotated_vector[1]),max(0,old_vector[1], rotated_vector[1])),
        ylim = c(min(0,old_vector[2], rotated_vector[2]),max(0,old_vector[2], rotated_vector[2])),
        xlab = "X", ylab = "y")
  lines(c(0, rotated_vector[1]), c(0, rotated_vector[2]), type="l", col = "red")}
  
}
programme_plot(pi/3, c(0,1)) 

在此处输入图片说明

在您的代码中几乎没有关于绘图的内容,所以我认为这是一个关于如何在基本图中绘制箭头的问题。

基础图形有一个功能arrows但首先你必须绘制一个坐标系,例如,

# make a playfield
plot(NA, xlim = c(-1, 1), ylim = c(-1, 1))

应该更改此命令以定义 x 和 y 限制、标签轴等。完成后,您可以使用arrows

arrows(x0=0, y0=0, x1=1, y1=0 col = "black")
arrows(x0=0, y0=0, x1=.707, y1=.707, col = "red")

形成诸如

在此处输入图片说明

暂无
暂无

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

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