plot_clinecurve: Plot the fitted cline for one or more test subjects using...

View source: R/plot_clinecurve.r

plot_clinecurveR Documentation

Plot the fitted cline for one or more test subjects using base R graphics.

Description

Plot the fitted cline for one or more test subjects using base R graphics.

Usage

plot_clinecurve(
  ggcline.object,
  cline.locus,
  locus.column = "locus",
  cline.col = "black",
  null.line.locus = NULL,
  null.line.col = "black",
  cline.centre.line = NULL,
  cline.centre.col = "black",
  ...
)

Arguments

ggcline.object

A gc object from ggcline.

cline.locus

Character vector. Names of the loci or ggcline test subjects for which to plot cline curves.

locus.column

Character string. Name of the field containing the test subjects. Default “locus”.

cline.col

Character vector. Colours for the plotted cline curves. Can be a single value to be applied to all curves or a vector the same length as cline.locus. Default “black”.

null.line.locus

Character vector. Names of loci for which to plot a black dashed line showing the null expectation. Default NULL.

null.line.col

Character vector. Colours for the plotted null dashed lines. Default “black” (repeated to the number of loci plotted).

cline.centre.line

Character vector. Names of ggcline test subjects (loci) for which to plot a vertical dashed line indicating the cline centre. Default NULL.

cline.centre.col

Character vector. Colours for the plotted centre lines.

...

Further graphical parameters.

Details

The function plots the cline curve(s), and genotype data (scaled from 0 to 1) can then be added as points. Samples from the parameter posteriors can be taken using the gghybrid function rtmvnormDT3, and their resulting clines added to the plot to indicate uncertainty, as described in the examples.

Value

a plot. No object is returned.

Author(s)

Richard Ian Bailey richardianbailey@gmail.com

Examples


## Not run: 
########################################################################
#Plot a genomic cline for one locus and add uncertainty and data points#
########################################################################

#Plot the curve for locus CLTA, using a previously created ggcline object (see examples for ggcline).
plot_clinecurve(
ggcline.object=gc$gc,
cline.locus="CLTA",
locus.column="locus",
cline.col="#E495A5",
cline.centre.line="CLTA",
cline.centre.col="black"
)

#Add a title and axis labels.
title(main = "CLTA",xlab="Hybrid index",ylab="Locus allele frequency",cex.main=1.5,cex.lab=1.5)

###################################################
#Add sampled cline curves to represent uncertainty#
###################################################

#There is no option for shading credible intervals within plot_clinecurve, so here is a suggestion for how to add some: 
#sample parameter values from the joint posterior, and add a grey curve to the plot for each posterior sample. 
#This will overlay the original cline curve plotted above, so it needs to be plotted again.

#First generate random samples from the posterior using gghybrid's rtmvnormDT3 function.

gcsamp=gc$gc[locus%in%c("CLTA"),
 rtmvnormDT3(
  nsamp=1000,                                #default is 1 sample per locus#
  meanlogv=mean_log_v,                       #no default, column must be specified#
  meanlogitcentre=mean_logit_centre,         #no default, column must be specified#
  varlogv=var_log_v,                         #no default, column must be specified#
  varlogitcentre=var_logit_centre,           #no default, column must be specified#
  covlogvlogitcentre=cov_log_v_logit_centre, #no default, column must be specified#
  lower=c(-Inf,-Inf),                        #default, not needed for sampling from the ggcline posterior but included in case it's useful in other contexts#
  upper=c(Inf,Inf)                           #default, not needed for sampling from the ggcline posterior but included in case it's useful in other contexts#
            ),                               #end of rtmvnormDT3 function#
 by="locus"];                                #indicate a grouping variable for the sampling#

#Add the best posterior estimates to gcsamp as the final row, so it's not obscured by the mass of grey lines.
locipost=gc$gc[locus=="CLTA",.(locus,mean_log_v,mean_logit_centre)];setnames(locipost,c("locus","log_v","logit_centre"));
gcsamp=rbind(gcsamp,locipost)

#Add the individual curves to the plot in a 'for' loop.
for(i in 1:nrow(gcsamp)){
        v = gcsamp[i,exp(log_v)]
        u = gcsamp[i,logit_centre*exp(log_v)]
        S1.prop_1 = gc$gc[locus=="CLTA",S1.prop_1];
        S0.prop_1 = gc$gc[locus=="CLTA",S0.prop_1];

        par(new=T);

if(i < nrow(gcsamp)){
        curve(S0.prop_1 + (x^v/(x^v + (1 - x)^v*exp(u)))*(S1.prop_1 - S0.prop_1), #The logit-logistic cline function#
	        from=0,to=1,axes=F,xlab="",ylab="", col="grey",lwd=0.5,ylim=c(0,1));
}else{                                                                            #Add the best-fitting curve to the plot again, so it's not obscured#
        curve(S0.prop_1 + (x^v/(x^v + (1 - x)^v*exp(u)))*(S1.prop_1 - S0.prop_1), #The logit-logistic cline function#
	        from=0,to=1,axes=F,xlab="",ylab="", col="#E495A5",lwd=3,ylim=c(0,1));
};
                         };#end of for loop#

######################################################
#Add data to the plot as genotypes scaled from 0 to 1#
######################################################

ploidy=2

#Create a data.table containing individual reference, hybrid index, locus name and genotype. See the ggcline examples for creating prepdata and hindlabel.
setkey(prepdata$data.prep,INDLABEL);setkey(hindlabel$hi,INDLABEL);
genodat=hindlabel$hi[,.(INDLABEL,h_posterior_mode)][prepdata$data.prep[locus=="CLTA",sum(Source_allele)/ploidy,by=c("INDLABEL","locus")]]

#Add the resulting points to the cline plot.
points(genodat[,V1]~genodat[,h_posterior_mode],pch=16,cex=0.7,col="#E495A5")


######################################################
#Plot cline curves for multiple loci on the same plot#
######################################################

#No data points or uncertainty included this time.

plot_clinecurve(
ggcline.object=gc$gc,
cline.locus=c("ACO1","EGR1","A2M","HSDL2","RPS4"),
locus.column="locus",
cline.col=c("orange","blue","green","red","magenta"),
null.line.locus=c("ACO1","EGR1","A2M","HSDL2","RPS4"),
null.line.col=c("orange","blue","green","red","magenta"),
cline.centre.line=c("ACO1","EGR1","A2M","HSDL2","RPS4"),
cline.centre.col=c("orange","blue","green","red","magenta")
)

#Add a title and axis labels:
title(main = "ACO1,EGR1,A2M,HSDL2,RPS4",xlab="Hybrid index",ylab="Locus allele frequency",cex.main=1.5,cex.lab=1.5)

## End(Not run)

ribailey/gghybrid documentation built on Feb. 2, 2024, 12:53 a.m.