morphospace | R Documentation |
This functions performs a "morphospace" of a dataset of two vectors: it connects extremal points to plot the largest polygon possible. This is a rather generic function and can be applied on datasets no matter of which are the vectors. The dataset just needs two vectors: firstly, plot the two vectors against each other (second vector in y-axis; first vector in x-axis), then run morphospace to superimpose a polygon to the plot.
morphospace(x,y,groups,plot.function,plot.points=TRUE,output="plot",
smoothing.method=NA,smoothing.param=NULL,
plot.new=FALSE,plot.new.opt=NULL,...)
x |
Either the |
y |
Has to be provided if and only if |
groups |
Optional. A factor vector defining subsets (or 'groups') in the dataset to plot separate morphospaces at once. |
plot.function |
Type of function used to plot the polygon: |
plot.points |
Logical. If the data points have to be plotted. Default is |
output |
Type of output, either the plot of the current dataset ( |
smoothing.method |
The smoothing method to use if the user wants a smoothed morphospace. See smooth for more details. |
smoothing.param |
The smoothing parameters to use if the user wants a smoothed morphospace. See smooth for more details. Must be a list with named elements, the names being the names of the parameters. |
plot.new |
If there has to be a new plot or if morphospace adds to a current plot. By default, it adds to the previous plot. |
plot.new.opt |
The options to be used if a new plot is added (if requested or if drawing smoothed morphospaces). Only works for the general frame of the plot (axis, labels etc). Must be a list of arguments. |
... |
graphical arguments, depend of the |
The function firstly identify the most extreme point on the x-axis, which is the starting point of the polygon.
Secondly, the function finds the second point of the polygon which is defined as having the steepest slope with the first point among all other points.
Thirdly, the function iteratively calculates the angle (using the same code than the function Angle
in the package LearnGeom
) between the slopes preceeding and succeeding the i-point (starting from the second defined point): the slope preceeding the i-point is unique (it is defined by the actual i point and the preceeding i-1 point), each slope succeeding the i-point is considered (with all other points). The largest angle is retained together with the associated i+1 point.
At the end, the function creates another data.frame of two vectors which each point surrounding the whole scatterplot; the function creates the polygon based on these points.
The smoothing of the morphospace is adapted from the StackExchange page Rasterizing coordinates for an irregular polygon changes original shape.
# For a simple morphospace
vec1_test<-sample.int(15,replace=TRUE)
vec2_test<-sample.int(15,replace=TRUE)
plot(vec1_test,vec2_test)
morphospace(vec1_test,vec2_test) # For a "raw" morphospace
morphospace(vec1_test,vec2_test,smoothing.method="spline",plot.points = FALSE) # For an example of smoothed (here, using splines) morphospace
morphospace(vec1_test,vec2_test,smoothing.method="spline",plot.points = TRUE) # Same, with the data
# For a morphospace with several subgroups
vec1_test<-c(runif(20,0,10),runif(20,10,20),runif(20,20,30))
vec2_test<-sample.int(60,replace=TRUE)
groups_test<-as.factor(c(rep("a",20),rep("b",20),rep("c",20)))
# For a "raw" morphospace
plot(vec1_test,vec2_test,type="n")
morphospace(vec1_test,vec2_test,groups_test,pch=21,col=c("red","green3","blue"),bg=c("red","green3","blue"))
# For an example of smoothed (here, using splines) morphospace
morphospace(vec1_test,vec2_test,groups_test,pch=21,col=c("red","green3","blue"),bg=c("red","green3","blue"),smoothing.method="spline")
# The same morphospace using the polygon function and its options to draw dotted semi-transparent lines
morphospace(vec1_test,vec2_test,groups_test,plot.function="polygon",pch=21,col=scales::alpha(c("red","green3","blue"),0.25),density=15,border=c("red","green3","blue"),col.points=c("red","green3","blue"),bg=c("red","green3","blue"),lty=2,plot.new=TRUE)
# The same morphospace using the polygon function and its options to draw semi-transparent convex hulls
morphospace(vec1_test,vec2_test,groups_test,plot.function="polygon",pch=21,col=scales::alpha(c("red","green3","blue"),0.1),border=c("red","green3","blue"),col.points=c("red","green3","blue"),bg=c("red","green3","blue"),lty=2,plot.new=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.