View source: R/rotate_landmarks.R
rotate_landmarks | R Documentation |
Rotates a single 2D or 3D landmark configuration about the origin of the coordinate system.
rotate_landmarks(LMdata, rotation, radians = TRUE, center = TRUE)
LMdata |
matrix k x p of k landmarks and p dimensions |
rotation |
vector containing the rotation angle(s) (a single rotation for 2D data, three rotations for 3D data) |
radians |
a logical on whether the angle(s) are provided in radians (default) or not |
center |
a logical on whether the rotation should be on the centered configuration |
This function can be useful to change the orientation of data for display purposes It could also be used to empirically check the rotation-invariance of an analysis. Notice that this function works on a single configuration of landmarks provided as a k x p matrix of k landmarks in p dimensions (i.e., p is 2 for 2D data and 3 for 3D data) As user-supplied rotation, the function expects a single number in the case of 2D data (rotation on the plane), a vector with three values (corresponding to rotation relative to the three axes) in 3D. If center=TRUE (default), first the configuration of landmarks is centered, then the rotation is performed, and finally the landmark coordinated are translated back to the original position. This accomplishes rotating the landmark configuration around its center
The function outputs a matrix k x p of the original configuration of landmarks rotated according to the user-supplied rotation
library(ggplot2)
Poly1=scale(t(matrix(c(4,1,3,1,1,2,2,3),nrow=2,ncol=4)),
center=TRUE, scale=FALSE)
# Create a polygon centered at the origin
Poly2=rotate_landmarks(LMdata=Poly1, rotation=10, radians=FALSE)
# Create a new polygon which is the rotated version of the first
# with respect to the origin (rotation of 10 degrees)
BothPolys4Plotting=as.data.frame(rbind(Poly1,Poly2))
BothPolys4Plotting[,3]=c(rep("Original",4),rep("Rotated",4))
BothPolys4Plotting[,4]=rep(1:4,2)
colnames(BothPolys4Plotting)=c("X","Y","Polygon","Landmark")
# Put them together in a way that is easy to plot in ggplot2
GraphLims=range(BothPolys4Plotting[,1:2])
# limits of the plot
ggplot() +
geom_polygon(data=BothPolys4Plotting, mapping=aes(x=X, y=Y, group=Polygon,fill=Polygon),alpha=0.5) +
geom_point(data=BothPolys4Plotting, aes(x=X, y=Y, color=Polygon)) +
geom_text(data=BothPolys4Plotting, aes(x=X, y=Y, label=Landmark), hjust=1, vjust=1, size=4)+
coord_fixed(ratio=1, xlim=GraphLims, ylim=GraphLims)+
theme_classic()
# Plot the two polygons (landmarks are numbered for ease of visualization)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.