简体   繁体   中英

Graphing Genotypes w/ R plot or hist

I am doing a very simple simulation using hardy-weinberg (for all you genetics junkies) and I am having a terrible time plotting out the frequencies of allele (0,1) frequencies and finally genotypes (0,1,2) frequencies over the course of a 100 generations. I am stuck trying to figure out R's matrices.

N = 30 # Size population in each line
lineN = 100 # Number of family lines
Genes0 = array(NA, dim=c(lineN, 2*N)) 

# Randomly form genotypes by sample function / 30:70 probabilites
# In sample x=c(0:1) represents a and A (30:70) alleles of a gene
for (i in 1:lineN) {
    Genes0[i, ] = sample(x=c(0:1), size=10, replace=T, prob=c(0.3,0.7)) 
}

generationN = 100
ParentGenes = Genes0
for (g in 1:generationN) {
  ChildGenes = array(NA, dim=c(lineN, 2*N))
  for (i in 1:lineN) {
    ChildGenes[i, ] = sample(ParentGenes[i, ], replace=T)
  }
}
  ParentGenes = ChildGenes
    table(ChildGenes)/(lineN*2*N) # Allele frequencies

    #Convert allele to genotypes: AA <=> 2; Aa / aA <=> 1; aa <=> 0.
    Genotypes = array(NA, dim=c(lineN, N))
    for  (j in 1:N) {
        Genotypes[, j] = ChildGenes[, 2*j-1] + ChildGenes[, 2*j]
    }
    table(Genotypes)/(lineN*N) # Genotype frequencies.

I don't know anything about genetics, so I'm not sure if I'm following but is this what you want:

tab <- do.call(rbind ,apply(ChildGenes, 1, function(x) table(x) / length(x)))

head(tab)

#             0         1
#[1,] 0.2500000 0.7500000
#[2,] 0.4000000 0.6000000
#[3,] 0.2833333 0.7166667
#[4,] 0.2500000 0.7500000
#[5,] 0.4833333 0.5166667
#[6,] 0.3666667 0.6333333

plot(1:100, tab[,1], col = "blue")
points(tab[,2], col = "red")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM