R 如何使用ggplot2在笛卡尔坐标系中绘制矢量?

R 如何使用ggplot2在笛卡尔坐标系中绘制矢量?,r,ggplot2,R,Ggplot2,我想画一个点p(2,4)和点Q(-4,-6)的向量。我可以很容易地在基本绘图系统中编写 arrows(0, 0, 2, 4, angle=20) # command to draw vector as arrows. arrows(0, 0, -4, -6, angle=20) # with angle of the arrow 但是,当我想将其转换为ggplot2时,我不知道如何继续 x1 <- c(0,2,-4) y1 <- c(0,4,-6) df <- data.f

我想画一个点p(2,4)和点Q(-4,-6)的向量。我可以很容易地在基本绘图系统中编写

arrows(0, 0, 2, 4, angle=20) # command to draw vector as arrows. 
arrows(0, 0, -4, -6, angle=20) # with angle of the arrow
但是,当我想将其转换为ggplot2时,我不知道如何继续

x1 <- c(0,2,-4)
y1 <- c(0,4,-6)
df <- data.frame(x1,y1)

ggplot(df) + 
    geom_point(aes(x = x1, y = y1))

x1您需要使用
geom_段
而不是
geom_点

library(ggplot2)
ggplot() + 
  geom_segment(aes(x = 0, y = 0, xend = 2, yend = 4)) + 
  geom_segment(aes(x = 0, y = 0, xend = -4, yend = -6))

您需要使用
geom\u段
而不是
geom\u点

library(ggplot2)
ggplot() + 
  geom_segment(aes(x = 0, y = 0, xend = 2, yend = 4)) + 
  geom_segment(aes(x = 0, y = 0, xend = -4, yend = -6))
以下是一个尝试:

      library(tidyverse)
      x=c(-6:6)
      y=c(-6:6)
      df %>% 
  ggplot(aes(x,y))+geom_abline(aes(slope=0,intercept=0))+
  annotate("text",x=2,y=4,label="P",size=9,colour="red")+
  annotate("text",x=-4,y=-6,label="Q",colour="red",size=9)+
  geom_vline(xintercept = 0)+
  geom_point(aes(x=0,y=0))+
  geom_segment(aes(x=0,y=0,xend=2,yend=4),
               arrow=arrow(length = unit(0.5,"cm"),angle=20),lineend = "butt")+
  geom_segment(aes(x=0,y=0,xend=-4,yend=-6),
               arrow=arrow(length = unit(0.5,"cm"),angle=20),lineend = "butt",linejoin = "round")+
  theme_minimal()
绘图: 以下是一个尝试:

      library(tidyverse)
      x=c(-6:6)
      y=c(-6:6)
      df %>% 
  ggplot(aes(x,y))+geom_abline(aes(slope=0,intercept=0))+
  annotate("text",x=2,y=4,label="P",size=9,colour="red")+
  annotate("text",x=-4,y=-6,label="Q",colour="red",size=9)+
  geom_vline(xintercept = 0)+
  geom_point(aes(x=0,y=0))+
  geom_segment(aes(x=0,y=0,xend=2,yend=4),
               arrow=arrow(length = unit(0.5,"cm"),angle=20),lineend = "butt")+
  geom_segment(aes(x=0,y=0,xend=-4,yend=-6),
               arrow=arrow(length = unit(0.5,"cm"),angle=20),lineend = "butt",linejoin = "round")+
  theme_minimal()
绘图:

在这种情况下,当前和预期绘图的图片将受到欢迎。除了
geom_point
,还有许多其他几何图形,请查看其他可能绘制线段的几何图形的参考:抱歉,@NelsonGon,因为我是stackoverflow的新手,我不知道如何粘贴预期绘图,我想画两个向量(线性代数),一个是从原点(0,0)到点(2,4),另一个是从原点(0,0)到点(-4,-6)@NelsonGon整个代码生成预期的绘图(0,0,xlim=c(-6,6),ylim=c(-6,6),main=“笛卡尔坐标系”)grid()abline(h=0)abline(v=0)text(2,4,“P”)text(-4,-6,“Q”)箭头(0,0,2,4,角度=20)#将矢量绘制为箭头的命令。箭头(0,0,-4,-6,角度=20)#在这种情况下,可以欣赏当前和预期绘图的图片。除了
geom_point
,还有许多其他几何图形,请查看其他可能绘制线段的几何图形的参考:对不起,@NelsonGon,因为我是stackoverflow的新手,我不知道如何粘贴预期图,我想画两个向量(线性代数),一个是从原点(0,0)到点(2,4),另一个是从原点(0,0)到点(-4,-6)@NelsonGon整个代码生成预期图(0,0,xlim=c(-6,6),ylim=c(-6,6),main=“笛卡尔坐标系”)网格()abline(h=0)abline(v=0)文本(2,4,“P”)文本(-4,-6,“Q”)箭头(0,0,2,4,角度=20)#将矢量绘制为箭头的命令。箭头(0,0,-4,-6,角度=20)#箭头角度感谢您的帮助,我想画两个向量(线性代数),一个是从原点(0,0)到点(2,4),另一个是从原点(0,0)到点(-4,-6),只需在绘图中添加一个新的
geom_段
调用即可。我将编辑答案。谢谢。很好,它完全解决了我的绘图问题谢谢你的帮助,我想画两个向量(线性代数),一个是从原点(0,0)到点(2,4),另一个是从原点(0,0)到点(-4,-6),只需在绘图中添加一个新的
geom_段
调用即可。我将编辑答案。谢谢。很好,它完全解决了我的绘图问题。看起来你错过了从原点到Q点的线段。这正是我想要的,非常感谢@NelsonGon@Otstats不客气。看起来你错过了从原点到Q点的片段。这正是我想要的,非常感谢@NelsonGon@Otstats不客气。