2

I want to draw a vector with point P(2,4) and point Q(-4,-6). I can easily write this in base plot system.

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

However, when I want to transform this to ggplot2, I don't know how to continue.

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

ggplot(df) + 
    geom_point(aes(x = x1, y = y1))
4
  • 1
    In this case, a picture of current and expected plots would be appreciated. Commented Jan 22, 2019 at 13:55
  • There are lots of other geoms besides geom_point, check out the reference for other possible geoms that might draw segments: ggplot2.tidyverse.org/reference Commented Jan 22, 2019 at 13:58
  • 1
    sorry, @NelsonGon , because I'm a new beginner to use the stackoverflow, I don't know how to paste the expected plots,I want to draw two vectors(linear algebra), one is from the origin(0,0) to point(2,4), another is from the origin(0,0) to point(-4,-6) Commented Jan 22, 2019 at 16:29
  • 1
    @NelsonGon the whole code to produced the expected plot plot(0, 0, xlim=c(-6,6), ylim=c(-6,6),main="Cartesian Coordinate System") grid() abline(h=0) abline(v=0) text(2,4,"P") text(-4,-6,"Q") 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 Commented Jan 22, 2019 at 16:32

3 Answers 3

3

Here is an attempt:

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

Plot: enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

looks like you're missing the segment from the origin to point Q.
It's exactly what I wanted, thanks a lot. @NelsonGon @ OTStats
You're Welcome.
1

You'll want to use geom_segment instead of geom_point:

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))

enter image description here

3 Comments

thank you for your help, I want to draw two vectors(linear algebra), one is from the origin(0,0) to point(2,4), another is from the origin(0,0) to point(-4,-6)
Simply add a new geom_segment call to your plot. I'll edit answer.
Thank you. Great job, it completely solved my plotting problem
0

You can use geom_vector from the ordr package.

Check here for the official documentation.

You can write:

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

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

Which prints:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.