Documentation/Labs/Plotting2DLineSegments

From Slicer Wiki
Revision as of 05:16, 7 February 2019 by Magalab (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Home < Documentation < Labs < Plotting2DLineSegments

We need a way to plot 2D arbitrary line segments to visualize 3D deformation vectors projected on each plane. Below is a code snippet that does this in R using a plotting function called segments which takes a starting coordinate set (x0,y0) and an end coordinate set (x1,y1) and plots a line segments. By convention (x0,y0) is the coordinate of that landmark in the meanshape space, and the segment drawn is the amount it will be displaced by the selected PC (which is also calculated by adding the eigenvectors times some arbitrary scale to the mean coordinate).


if (require(Morpho)) {} else install.packages('Morpho')
if (require(geomorph)) {} else install.packages('geomorph')


#define the location to save the temp zipfile 
zip = 'c:/tmp/Gorilla_Skull_LMs.zip'
download.file(url = 'https://github.com/SlicerMorph/SampleData/blob/master/Gorilla_Skull_LMs.zip?raw=true', 
destfile = zip, method = 'auto', mode='wb')
files = unzip(zip, list = T)$Name

no.subjects = length(files)
no.landmarks = nrow (Morpho::read.fcsv(unzip(zip, files[1])))

lms = array(dim = c(no.landmarks, 3, no.subjects))
            
for (i in 1:no.subjects)  lms[,,i] = Morpho::read.fcsv(unzip(zip, files=files[i]))

gpa = geomorph::gpagen(lms)
pca = geomorph::plotTangentSpace(gpa$coords)

par(mfrow=c(2,2))
par(pty="s")

plot(gpa$consensus[,c(1,2)], pch=20, main ='mean shape')
plot(gpa$consensus[,c(1,2)],pch=20, main = 'XY plane', asp=1)
for (i in 1:41) segments(x0=gpa$consensus[i,1], y0=gpa$consensus[i,2], x1=pca$pc.shapes$PC1max[i,1], y1=pca$pc.shapes$PC1max[i,2], col='red')
for (i in 1:41) segments(x0=gpa$consensus[i,1], y0=gpa$consensus[i,2], x1=pca$pc.shapes$PC2max[i,1], y1=pca$pc.shapes$PC2max[i,2], col='green')

plot(gpa$consensus[,c(1,3)],pch=20, main = 'XZ plane', asp=1)
for (i in 1:41) segments(x0=gpa$consensus[i,1], y0=gpa$consensus[i,3], x1=pca$pc.shapes$PC1max[i,1], y1=pca$pc.shapes$PC1max[i,3], col='red')
for (i in 1:41) segments(x0=gpa$consensus[i,1], y0=gpa$consensus[i,3], x1=pca$pc.shapes$PC2max[i,1], y1=pca$pc.shapes$PC2max[i,3], col='green')

plot(gpa$consensus[,c(2,3)],pch=20, main = 'YZ plane', asp=1)
for (i in 1:41) segments(x0=gpa$consensus[i,2], y0=gpa$consensus[i,3], x1=pca$pc.shapes$PC1max[i,2], y1=pca$pc.shapes$PC1max[i,3], col='red')
for (i in 1:41) segments(x0=gpa$consensus[i,2], y0=gpa$consensus[i,3], x1=pca$pc.shapes$PC2max[i,2], y1=pca$pc.shapes$PC2max[i,3], col='green')
Sample Output