user24318
user24318

Reputation: 485

draw lines on overlaid scatter plots in R

enter image description hereI want to visualize an embedding from R^2 to R^2. I have overlaid the scatter plots of the original data and the transformed data. I want to join the dots with the stars corresponding to same color with a line so that I can see how each point gets transformed. Any idea about how to do this in R. A reproducible example of overlaid scatter plot is as given below.

set.seed(18)
 M<-cbind(a=runif(10),b=runif(10))
 N<-cbind(d=rnorm(10),e=rnorm(10))
plot(N[,1],N[,2],col=rainbow(10),pch=20,xlab="x",ylab="y")
points(M[,1],M[,2],col=rainbow(10), pch=8)

Upvotes: 1

Views: 162

Answers (2)

Andrew
Andrew

Reputation: 743

Here's another way using ggplot2:

library(ggplot2)

set.seed(18)
data <- data.frame(
    a = runif(10),
    b = runif(10),
    d = rnorm(10),
    e = rnorm(10)
)

ggplot(data, aes(x = a, xend = d, y = b, yend = e)) +
    geom_segment(arrow = arrow(ends = "last")) +
    xlab("x") + ylab("y")

Example Plot

Upvotes: 1

G5W
G5W

Reputation: 37641

One way is with segments

segments(N[,1], N[,2], M[,1], M[,2], col = rainbow(10))

enter image description here

Upvotes: 3

Related Questions