2
votes

Suppose that I have the following datasets:

x1 <- c(1.1,1.2,2.6,1.7,1.0,1.4)
y1 <- c(0.4,0.6,0.8,0.5,0.7,0.8)
x2 <- c(1.2,1.7,2.0,1.9,1.2,1.4)
y2 <- c(0.5,0.6,0.8,0.8,0.7,0.4)
T1 <- data.frame(x=x1,y=y1)
T2 <- data.frame(x=x2,y=y2)

A) How to plot these two different data frames (T1 and T2) in a scatter plot representing T1 with points and the displacement from T1 to T2 with lines for each row in ggplot2?

B) Is it possible to represent the lines with an arrow pointing toward the displacement direction?

Many thanks!

1

1 Answers

3
votes

I'd recommend combining T1 and T2 in one data frame:

dat <- cbind(T1, T2)
names(dat) <- c("x1", "y1", "x2", "y2")

Then you can plot the points of T1 and add arrows with geom_segment and the T2 values as end points:

ggplot(dat) +
  geom_point(aes(x = x1, y = y1)) +
  geom_segment(aes(x = x1, xend = x2, y = y1, yend = y2), arrow = arrow())

enter image description here