Chapter 10
FIGURES
Figures
10.1 and 10.2 use a function "number2words" written by John Fox (see
below) and "capitalize" that you will need to copy to your workspace.
Figure
10.7 requires that you download the "car" package. To do so, get
into R and go to the top menu, then "packages", "install package(s)",
pick a "mirror" close to you (e.g., "USA (IA)"), and then select "car"
from the list.
number2words=function(x){
# Written by John Fox
# see http://socserv.socsci.mcmaster.ca/jfox/Courses/R-programming/Basic-programming.R
makeDigits=function(x) strsplit(as.character(x), "")[[1]]
makeNumber=function(x) as.numeric(paste(x, collapse=""))
ones=structure(c("zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"), .Names = c("0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"))
suffixes=c("thousand,", "million,", "billion,", "trillion,")
teens=structure(c("ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", " seventeen", "eighteen", "nineteen"), .Names = c("0",
"1", "2", "3", "4", "5", "6", "7", "8", "9"))
tens=structure(c("twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"), .Names = c("2", "3", "4", "5", "6", "7",
"8", "9"))
trim=function(text){
gsub("(^\ *)|((\ *|-|,\ zero|-zero)$)",
"", text)
}
negative <- x < 0
x <- abs(x)
digits <- makeDigits(x)
nDigits <- length(digits)
result <- if (nDigits == 1) as.vector(ones[digits])
else if (nDigits == 2)
if (x <= 19) as.vector(teens[digits[2]])
else trim(paste(tens[digits[1]], "-", ones[digits[2]], sep=""))
else if (nDigits == 3) {
tail <- makeNumber(digits[2:3])
if (tail == 0) paste(ones[digits[1]], "hundred")
else trim(paste(ones[digits[1]], "hundred", number2words(tail)))
}
else {
nSuffix <- ((nDigits + 2) %/% 3) - 1
if (nSuffix > length(suffixes) || nDigits > 15)
stop(paste(x, "is too large!"))
pick <- 1:(nDigits - 3*nSuffix)
trim(paste(number2words(makeNumber(digits[pick])),
suffixes[nSuffix], number2words(makeNumber(digits[-pick]))))
}
if (negative) paste("minus", result) else result
}
capitalize=function (string)
{
capped <- grep("^[^A-Z]*$", string, perl = TRUE)
substr(string[capped], 1, 1) <- toupper(substr(string[capped],
1, 1))
return(string)
}
Fig10.1=function (nloc=2,p=0.5,mean.x=72,sa=2)
{
# nloc is number of loci
# p is allele frequency at each locus
# mean.x is the mean of the measurement
# sa is the additive genetic standard deviation. There is no environ var, so sa
# is also the phenotypic standard deviation
nalls=2*nloc
F.cum=dbinom(0:nalls,nalls,p)
a=sa/sqrt(2*nloc*p*(1-p))
x=(0:nalls)*a
mu=as.vector(x%*%F.cum)
x=x+mean.x-mu
top=max(F.cum)
plot(x,F.cum,type='h',xlab='Stature (inches)',ylab='Frequency',
main=noquote(paste(capitalize(number2words(nloc)),ifelse(nloc<2,'locus','loci'),'(B allele = ',round(a,2),')')),
ylim=c(0,top),xlim=c(mean.x-3*sa,mean.x+3*sa),axes=F)
box();axis(1);axis(2)
}