Transfering line segments from a plot to another with corresponding points on different coordinates

117 views Asked by At

Due to its nature it is probably unique.

So I've a plot (specifically it is a classical MultiDimensional Scaling plot). Typically, I have a data frame imported in R but, I will use randomly generated data here so this is the script to get my MDS:

a<-data.frame(replicate(10,sample(0:7,300,rep=TRUE)))
d <- dist(a)
fit <- cmdscale(d,eig=TRUE, k=2)
fit
x <- fit$points[,1]
y <- fit$points[,2]
plot(x, y, xlab="PC1", ylab="PC2",main="Metric MDS", type="n")
text(x, y, labels = row.names(a), cex=.7)

Then I wanted to draw lines between all points with each other. I've done that somewhat awkwardly:

myd<-data.frame(x,y,labels=row.names(a))
plot(myd$x, myd$y)
apply(combn(seq_len(nrow(myd)), 2), 2, 
function(x) lines(myd[x, ]$x, myd[x, ]$y))

Then I created a simple 3d scatterplot out of another set of data:

b<-data.frame(replicate(3,sample(0:30,300,rep=TRUE)))
plot3d(b$X1,b$X2,b$X3)

& got the familiar 3D scatter-plot which has the same number of points as the MDS plot-essentially every point of the 3D scatter-plot corresponds to a point on the MDS plot.

My question has 2 parts:

a) At the part where I drew the line segments on the MDS plot. Is there any way to specify a threshold as to when to draw a line i.e. when the euclidean distance between 2 points is below a specified value of my preference?

b) How to transfer or simulate the line segments joining the points in the MDS plot on the 3d scatter-plot among their corresponding points?

Note:

I can't do this manually by joining pairs of points one by one because that will take ages.
So I need a way to tell R to make the correspondence between 2 different sets of coordinates (one from the MDS plot) and the other from the data frame from which, I made the 3d scatter-plot. And of course I need to tell R to retain the 'distance' attribute between 2 points (i.e. the line-segment length) as a different attribute e.g. line color or thickness in order to make sense in the 3d scatter-plot because otherwise it won't mean anything.

0

There are 0 answers