From ab1d968603e2baddb588ef86b4501b0518d14ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Opstad?= Date: Fri, 19 Nov 2021 19:05:11 +0100 Subject: [PATCH] Add optional label parameters to the evolution functions, to support translating plots to other languages. --- R/evolution.R | 18 +++++++++++++++--- man/drift.selection.Rd | 2 +- man/genetic.drift.Rd | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/R/evolution.R b/R/evolution.R index b840c8b..1471a87 100644 --- a/R/evolution.R +++ b/R/evolution.R @@ -304,6 +304,10 @@ plot.coalescent.plot<-function(x,...){ drift.selection<-function(p0=0.5,Ne=100,w=c(1,1,1),ngen=400,nrep=10, colors=NULL,...){ if(is.null(colors)) colors<-rainbow(nrep) + if(hasArg(xlabel)) xlabel=list(...)$xlabel + else xlabel<-"Time (generations)" + if(hasArg(ylabel)) ylabel=list(...)$ylabel + else ylabel<-"f(A)" w<-(w/max(w))[3:1] gametes<-rep(0,2*Ne) if(p0>0) gametes[1:round(p0*2*Ne)]<-1 @@ -324,7 +328,7 @@ drift.selection<-function(p0=0.5,Ne=100,w=c(1,1,1),ngen=400,nrep=10, for(j in 1:nrep) p[[j]][i+1]<-mean(gametes[[j]]) } plot(0:ngen,p[[1]],type="l",col=colors[1],lwd=2,ylim=c(0,1), - xlab="time (generations)",ylab="f(A)") + xlab=xlabel,ylab=ylabel) if(nrep>1) nulo<-mapply(lines,x=replicate(nrep-1,0:ngen,simplify=FALSE), y=p[2:nrep],col=colors[2:nrep],lwd=2) @@ -356,8 +360,12 @@ plot.drift.selection<-function(x,...){ else type<-"l" if(hasArg(lwd)) lwd<-list(...)$lwd else lwd<-2 + if(hasArg(xlabel)) xlabel=list(...)$xlabel + else xlabel<-"Time (generations)" + if(hasArg(ylabel)) ylabel=list(...)$ylabel + else ylabel<-"f(A)" plot(0:ngen,x[[1]],type=type,col=colors[1],lwd=lwd,ylim=c(0,1), - xlab="time (generations)",ylab="f(A)") + xlab=xlabel,ylab=ylabel) if(nrep>1) nulo<-mapply(lines,x=replicate(nrep-1,0:ngen,simplify=FALSE), y=x[2:nrep],col=colors[2:nrep],type=type,lwd=lwd) @@ -370,6 +378,10 @@ msd<-function(p0=c(0.5,0.5),Ne=c(100,100), colors=c("red","blue"),...){ if(hasArg(show.legend)) show.legend=list(...)$show.legend else show.legend<-TRUE + if(hasArg(xlabel)) xlabel=list(...)$xlabel + else xlabel<-"Time (generations)" + if(hasArg(ylabel)) ylabel=list(...)$ylabel + else ylabel<-"f(A)" w<-lapply(w,function(w) (w/max(w))[3:1]) gametes<-lapply(Ne,function(Ne) rep(0,2*Ne)) gametes<-mapply(function(p0,g,N){ @@ -404,7 +416,7 @@ msd<-function(p0=c(0.5,0.5),Ne=c(100,100), for(j in 1:2) p[[j]][i+1]<-mean(gametes[[j]]) } plot(0:ngen,p[[1]],type="l",col=colors[1],lwd=2,ylim=c(0,1), - xlab="time (generations)",ylab="f(A)") + xlab=xlabel,ylab=ylabel) lines(x=0:ngen,y=p[[2]],col=colors[2],lwd=2) if(show.legend) legend(x="topright",legend=1:2,lty=1,col=colors, lwd=2,bg=make.transparent("white",0.8)) diff --git a/man/drift.selection.Rd b/man/drift.selection.Rd index e89b5f0..48db493 100644 --- a/man/drift.selection.Rd +++ b/man/drift.selection.Rd @@ -11,7 +11,7 @@ \item{nrep}{number of replicate simulations.} \item{ngen}{total time, in number of generations, for the simulation.} \item{colors}{colors to use for plotting.} - \item{...}{optional arguments. Presently the only arguments are \code{type} (e.g., \code{"l"}, \code{"s"}) and \code{lwd} in the \code{plot} method.} + \item{...}{optional arguments. Presently the only arguments are \code{type} (e.g., \code{"l"}, \code{"s"}) and \code{lwd} in the \code{plot} method. \code{xlabel} and \code{ylabel} sets the labels for the x and y axes, respectively.)} } \description{ Simulates drift and natural selection at a single biallelic locus within one or various populations. diff --git a/man/genetic.drift.Rd b/man/genetic.drift.Rd index d21e297..e4ac5bf 100644 --- a/man/genetic.drift.Rd +++ b/man/genetic.drift.Rd @@ -11,7 +11,7 @@ \item{time}{Total time, in number of generations, for the simulation.} \item{show}{Various options for plotting. \code{"p"} shows the frequency of the A allele through time; \code{"genotypes"} creates an animated histogram with the frequencies of each of the three genotypes through time; \code{"fixed"} shows the fraction of populations that have fixed for each allele, a or A; \code{"heterozygosity"} plots the mean heterozygosity and the expected heterozygosity through time. The default is \code{show="p"}.} \item{pause}{Pause between generations. \code{pause=0.01} (for instance) might smooth animation.} - \item{...}{optional arguments. In \code{genetic.drift} the optional arguments are presently: \code{colors} (a vector giving the colors to be used to graph the various simulations); and \code{lwd}. The \code{plot} method of the object class adds the optional argument \code{type} (e.g., \code{"l"} or \code{"s"}.)} + \item{...}{optional arguments. In \code{genetic.drift} the optional arguments are presently: \code{colors} (a vector giving the colors to be used to graph the various simulations); and \code{lwd}. The \code{plot} method of the object class adds the optional argument \code{type} (e.g., \code{"l"} or \code{"s"}. \code{xlabel} and \code{ylabel} sets the labels for the x and y axes, respectively.)} } \description{ This function simulates genetic drift at a biallelic genetic locus with no selection and no mutation in a sexually reproducing diploid population or set of populations. It is essentially redundant with \code{\link{drift.selection}}, but in which there is no difference in relative fitness among genotypes; however, it also allows the user to visualize heterozygosity or genetic variation through time - options that are not yet implemented in \code{\link{drift.selection}}.