Difference between revisions of "Documentation/Labs/Plotting2DLineSegments"
From Slicer Wiki
(Description of 2D line segments need) |
|||
Line 2: | Line 2: | ||
− | + | <pre> | |
− | |||
install.packages(c('Morpho', 'geomorph')) | install.packages(c('Morpho', 'geomorph')) | ||
+ | #define the location to save the temp zipfile | ||
zip = 'c:/tmp/Gorilla_Skull_LMs.zip' | 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') | + | 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 | files = unzip(zip, list = T)$Name | ||
Line 35: | Line 36: | ||
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$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') | 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') | ||
− | + | </pre> | |
[[File:Rplot02.png|thumb|left|Sample Output]] | [[File:Rplot02.png|thumb|left|Sample Output]] |
Revision as of 18:36, 6 February 2019
Home < Documentation < Labs < Plotting2DLineSegmentsWe 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).
install.packages(c('Morpho', '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')