This code demonstrates how to create a plot that displays the spread of physiological data for each genotype, the mean value for each genotype and the predicted trendline for each inheritance model.
MiceData <- read.delim("homework2.data.txt")
MiceData$LogCD4CD8Ratio <- (log(MiceData$CD4CD8Ratio))^0.75
Includes the addition of dominant and recessive genotype dosages to the data frame
MiceData$DominantDosage <- ifelse(MiceData$mCV22965443.genotype=="AA", 2, 0) + ifelse(MiceData$mCV22965443.genotype=="TA", 2, 0)
MiceData$RecessiveDosage <- ifelse(MiceData$mCV22965443.genotype=="AA", 2, 0)
Ma <- lm(LogCD4CD8Ratio ~ mCV22965443.dosage, data = MiceData)
Md <- lm(LogCD4CD8Ratio ~ DominantDosage, data = MiceData)
Mr <- lm(LogCD4CD8Ratio ~ RecessiveDosage, data = MiceData)
meanMa2 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$mCV22965443.dosage == "2")])
meanMa1 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$mCV22965443.dosage == "1")])
meanMa0 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$mCV22965443.dosage == "0")])
meanMd1 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$DominantDosage == "2")])
meanMd0 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$DominantDosage == "0")])
meanMr1 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$RecessiveDosage == "2")])
meanMr0 <- mean(MiceData$LogCD4CD8Ratio[which(MiceData$RecessiveDosage == "0")])
plot(LogCD4CD8Ratio ~ mCV22965443.dosage, data = MiceData, axes = FALSE, xlab = "Genotype", ylab = "Log(CD4+/CD8+ Ratio) ^3/4")
axis(1, at = 0:2, labels = c("TT", "TA", "AA"))
axis(2, at = c(0.0, 0.5, 1.0, 1.5, 2.0), labels = c("0.0", "0.5", "1.0", "1.5", "2.0"))
segments(x0 = 1.8, x1 = 2.2, y0 = meanMa2, y1 = meanMa2, col = "#1B9E77", lwd = 2)
segments(x0 = 0.8, x1 = 1.2, y0 = meanMa1, y1 = meanMa1, col = "#1B9E77", lwd = 2)
segments(x0 = -0.2, x1 = 0.2, y0 = meanMa0, y1 = meanMa0, col = "#1B9E77", lwd = 2)
segments(x0 = 1.8, x1 = 2.2, y0 = meanMd1, y1 = meanMd1, col = "#D95F02", lwd = 2)
segments(x0 = 0.8, x1 = 1.2, y0 = meanMd1, y1 = meanMd1, col = "#D95F02", lwd = 2)
segments(x0 = -0.2, x1 = 0.2, y0 = meanMd0, y1 = meanMd0, col = "#D95F02", lty = 2, lwd = 2)
segments(x0 = 1.8, x1 = 2.2, y0 = meanMr1, y1 = meanMr1, col = "#E6AB02", lty = 2, lwd = 2)
segments(x0 = 0.8, x1 = 1.2, y0 = meanMr0, y1 = meanMr0, col = "#E6AB02", lwd = 2)
segments(x0 = -0.2, x1 = 0.2, y0 = meanMr0, y1 = meanMr0, col = "#E6AB02", lwd = 2)
abline(Ma, col = "#1B9E77", lty = 3, lwd = 1)
abline(Md, col = "#D95F02", lty = 3, lwd = 1)
abline(Mr, col = "#E6AB02", lty = 3, lwd = 1)
legend("topleft", title = "Means", legend = c("Additive Model", "Dominant Model", "Recessive Model"), col = c("#1B9E77", "#D95F02", "#E6AB02"), lty = 1, lwd = 2, cex = 0.8)
legend(x = 0.54, title = "Regression Lines", y = 1.838, legend = c("Additive Model", "Dominant Model", "Recessive Model"), col = c("#1B9E77", "#D95F02", "#E6AB02"), lty = 3, lwd = 1, cex = 0.8)